Class: Spikard::App

Inherits:
Object
  • Object
show all
Defined in:
lib/spikard/app.rb,
lib/spikard/service.rb,
sig/types.rbs

Overview

Spikard application builder.

Constant Summary collapse

VALID_METHODS =

Valid HTTP methods

Set.new(%w[GET POST PUT PATCH DELETE HEAD OPTIONS CONNECT TRACE]).freeze
SPECIAL_PARAM_NAMES =

Parameter names that are never treated as request-derived kwargs

Set.new(%w[self]).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



46
47
48
49
50
51
# File 'lib/spikard/app.rb', line 46

def initialize(config = nil)
  # Create a low-level App via the native extension interface
  # The low-level App is already available in the Spikard namespace
  @registrations = []
  @config = config
end

Class Method Details

.defaultApp

Returns:



277
# File 'sig/types.rbs', line 277

def self.default: () -> App

.newApp

Returns:



276
# File 'sig/types.rbs', line 276

def self.new: () -> App

Instance Method Details

#add_route(method, path, handler, body_type: nil) ⇒ Object

Register a handler for method and path and return it unchanged.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spikard/app.rb', line 60

def add_route(method, path, handler, body_type: nil)
  method_upper = method.upcase
  raise "Unsupported HTTP method: #{method.inspect}" unless VALID_METHODS.include?(method_upper)

  spec = Introspection.introspect(handler, method_upper, path, body_type)

  # Create a RouteBuilder and attach schemas for Rust-side validation.
  # The native RouteBuilder.new accepts the lowercase HTTP method string
  # (e.g. "post") and maps it to the core Method internally.
  builder = create_route_builder_with_schemas(method_upper.downcase, path, spec)

  # Build the adapter that bridges Rust RequestData → handler kwargs → response
  adapter = make_adapter(spec)

  # Store as a "route" registration with the RouteBuilder and adapter
  @registrations.push(["route", [builder], adapter])

  handler
rescue StandardError => e
  raise "Failed to register route #{method_upper} #{path}: #{e.message}"
end

#attach_axum_routerApp

Parameters:

  • router (String)

Returns:



273
# File 'sig/types.rbs', line 273

def attach_axum_router: (String router) -> App

#config(config) ⇒ Object

Set the server configuration and return self for chaining.



54
55
56
57
# File 'lib/spikard/app.rb', line 54

def config(config)
  @config = config
  self
end

#connect(path: String, &block) ⇒ Object

Register a CONNECT route.



123
124
125
# File 'lib/spikard/app.rb', line 123

def connect(path, &block)
  add_route("CONNECT", path, block)
end

#delete(path: String, &block) ⇒ Object

Register a DELETE route with optional body DTO class.



103
104
105
# File 'lib/spikard/app.rb', line 103

def delete(path, body: nil, &block)
  add_route("DELETE", path, block, body_type: body)
end

#get(path: String, &block) ⇒ Object

Register a GET route.



83
84
85
# File 'lib/spikard/app.rb', line 83

def get(path, &block)
  add_route("GET", path, block)
end

#head(path: String, &block) ⇒ Object

Register a HEAD route.



108
109
110
# File 'lib/spikard/app.rb', line 108

def head(path, &block)
  add_route("HEAD", path, block)
end

#into_routerString

Build the underlying Axum router (for embedding/testing).

Returns:

  • (String)


138
139
140
# File 'lib/spikard/app.rb', line 138

def into_router
  Spikard.app_into_router(@registrations)
end

#into_router_and_configString

Returns:

  • (String)


275
# File 'sig/types.rbs', line 275

def into_router_and_config: () -> String

#merge_axum_routerApp

Parameters:

  • router (String)

Returns:



272
# File 'sig/types.rbs', line 272

def merge_axum_router: (String router) -> App

#on_errorApp

Parameters:

  • hook (String)

Returns:



269
# File 'sig/types.rbs', line 269

def on_error: (String hook) -> App

#on_requestApp

Parameters:

  • hook (String)

Returns:



265
# File 'sig/types.rbs', line 265

def on_request: (String hook) -> App

#on_responseApp

Parameters:

  • hook (String)

Returns:



268
# File 'sig/types.rbs', line 268

def on_response: (String hook) -> App

#options(path: String, &block) ⇒ Object

Register an OPTIONS route.



113
114
115
# File 'lib/spikard/app.rb', line 113

def options(path, &block)
  add_route("OPTIONS", path, block)
end

#patch(path: String, &block) ⇒ Object

Register a PATCH route with optional body DTO class.



98
99
100
# File 'lib/spikard/app.rb', line 98

def patch(path, body: nil, &block)
  add_route("PATCH", path, block, body_type: body)
end

#post(path: String, &block) ⇒ Object

Register a POST route with optional body DTO class.



88
89
90
# File 'lib/spikard/app.rb', line 88

def post(path, body: nil, &block)
  add_route("POST", path, block, body_type: body)
end

#pre_handlerApp

Parameters:

  • hook (String)

Returns:



267
# File 'sig/types.rbs', line 267

def pre_handler: (String hook) -> App

#pre_validationApp

Parameters:

  • hook (String)

Returns:



266
# File 'sig/types.rbs', line 266

def pre_validation: (String hook) -> App

#put(path: String, &block) ⇒ Object

Register a PUT route with optional body DTO class.



93
94
95
# File 'lib/spikard/app.rb', line 93

def put(path, body: nil, &block)
  add_route("PUT", path, block, body_type: body)
end

#register_graphql_routeApp

Parameters:

Returns:



270
# File 'sig/types.rbs', line 270

def register_graphql_route: (String path, Method method, String schema_type, SchemaConfig config) -> App

#register_graphql_sdl_routeApp

Parameters:

Returns:



271
# File 'sig/types.rbs', line 271

def register_graphql_sdl_route: (String path, Method method, String sdl, json_value response_data, DynamicSchemaConfig config) -> App

#register_route(builder, handler) ⇒ Object



29
30
31
32
33
# File 'lib/spikard/service.rb', line 29

def register_route(builder, handler)
  # Register a route callback directly without block syntax.
  @registrations.push(["route", [builder], handler])
  self
end

#route(builder, &block) ⇒ Object

Register a route with an explicit HTTP method (defaults to GET).



128
129
130
# File 'lib/spikard/app.rb', line 128

def route(path, method: "GET", body: nil, &block)
  add_route(method, path, block, body_type: body)
end

#runObject

Run the HTTP server using the configured routes.



133
134
135
# File 'lib/spikard/app.rb', line 133

def run
  Spikard.app_run(@registrations)
end

#trace(path: String, &block) ⇒ Object

Register a TRACE route.



118
119
120
# File 'lib/spikard/app.rb', line 118

def trace(path, &block)
  add_route("TRACE", path, block)
end