Class: Spikard::App
- Inherits:
-
Object
- Object
- Spikard::App
- 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
-
#add_route(method, path, handler, body_type: nil) ⇒ Object
Register a handler for method and path and return it unchanged.
- #attach_axum_router ⇒ App
-
#config(config) ⇒ Object
Set the server configuration and return self for chaining.
-
#connect(path: String, &block) ⇒ Object
Register a CONNECT route.
-
#delete(path: String, &block) ⇒ Object
Register a DELETE route with optional body DTO class.
-
#get(path: String, &block) ⇒ Object
Register a GET route.
-
#head(path: String, &block) ⇒ Object
Register a HEAD route.
-
#initialize ⇒ App
constructor
A new instance of App.
-
#into_router ⇒ String
Build the underlying Axum router (for embedding/testing).
- #into_router_and_config ⇒ String
- #merge_axum_router ⇒ App
- #on_error ⇒ App
- #on_request ⇒ App
- #on_response ⇒ App
-
#options(path: String, &block) ⇒ Object
Register an OPTIONS route.
-
#patch(path: String, &block) ⇒ Object
Register a PATCH route with optional body DTO class.
-
#post(path: String, &block) ⇒ Object
Register a POST route with optional body DTO class.
- #pre_handler ⇒ App
- #pre_validation ⇒ App
-
#put(path: String, &block) ⇒ Object
Register a PUT route with optional body DTO class.
- #register_graphql_route ⇒ App
- #register_graphql_sdl_route ⇒ App
- #register_route(builder, handler) ⇒ Object
-
#route(builder, &block) ⇒ Object
Register a route with an explicit HTTP method (defaults to GET).
-
#run ⇒ Object
Run the HTTP server using the configured routes.
-
#trace(path: String, &block) ⇒ Object
Register a TRACE route.
Constructor Details
#initialize ⇒ App
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
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.}" end |
#attach_axum_router ⇒ App
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_router ⇒ String
Build the underlying Axum router (for embedding/testing).
138 139 140 |
# File 'lib/spikard/app.rb', line 138 def into_router Spikard.app_into_router(@registrations) end |
#into_router_and_config ⇒ String
275 |
# File 'sig/types.rbs', line 275
def into_router_and_config: () -> String
|
#merge_axum_router ⇒ App
272 |
# File 'sig/types.rbs', line 272
def merge_axum_router: (String router) -> App
|
#options(path: String, &block) ⇒ Object
Register an OPTIONS route.
113 114 115 |
# File 'lib/spikard/app.rb', line 113 def (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 |
#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_route ⇒ App
270 |
# File 'sig/types.rbs', line 270
def register_graphql_route: (String path, Method method, String schema_type, SchemaConfig config) -> App
|
#register_graphql_sdl_route ⇒ App
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 |
#run ⇒ Object
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 |