-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the BRRTRouter wiki!
Purpose: Single reference for what BRRTRouter is, the concepts behind it, how it works, the core components of the codebase, and how the pieces fit together. For deeper flows and code-generation details, see ARCHITECTURE.md and RequestLifecycle.md.
- What is BRRTRouter?
- Concepts
- How It Works (Two Flows)
- Core Components of the Codebase
- Component / Architecture Graph
- Request Flow: Sequence Diagram
- References
BRRTRouter is a high-performance, OpenAPI-driven HTTP router for Rust. It does two main things:
- Code generation — Turns an OpenAPI 3.1 specification into a complete, runnable Rust service (handlers, controllers, config, registry).
-
Runtime — Serves HTTP by matching requests to handlers, validating input/output, enforcing security, and dispatching work to coroutines (lightweight threads via the
mayruntime).
So: you define your API once in OpenAPI; BRRTRouter gives you a type-safe server with routing, validation, auth, CORS, metrics, and observability out of the box. No hand-written route tables or per-endpoint glue.
In short: OpenAPI spec → generated service + runtime that routes, validates, authenticates, and runs your handlers in coroutines.
| Concept | Description |
|---|---|
| OpenAPI as source of truth | Paths, methods, parameters, request/response schemas, and security are read from the spec. Routing, validation, and generated code all derive from it. |
| RouteMeta | Internal representation of one route: path, method, handler name, parameters, security, CORS, etc. Built from the spec by spec::build_routes. |
| Router | Compiles paths into matchers (regex and/or radix tree). At runtime, takes method + path and returns a RouteMatch (handler name + path/query params). |
| Dispatcher | Looks up the handler by name and sends the request into a handler coroutine via a channel. Collects the response and returns it. Uses a worker pool and MPSC/MPMC channels. |
| Middleware chain | Request/response pipeline: CORS → tracing → auth → metrics → … . Each step can modify the request/response or short-circuit (e.g. 401/403). |
| Security providers | Pluggable auth: API key, JWT/JWKS, OAuth2, remote API key. Registered from OpenAPI securitySchemes and invoked by auth middleware. |
| Validation | Request parameters and body are validated against the OpenAPI schema; response can be validated too. Failures return RFC 7807 Problem Details. |
| Typed handlers | Generated or hand-written handlers receive a HandlerRequest (or a typed TryFrom<HandlerRequest> request) and return a HandlerResponse. |
| Code generation | CLI brrtrouter-gen loads the spec, builds routes, analyzes schemas, renders Askama templates, and writes a full project (main, registry, handlers, controllers, config). |
| Hot reload | Optional: watch the OpenAPI file and rebuild the router + re-register routes without restarting the process. |
-
Load spec —
spec::load_specreads and parses the OpenAPI YAML/JSON. -
Build routes —
spec::build_routesturns paths and operations intoRouteMeta(handler names, parameters, security, etc.). -
Analyze schemas —
generator::schemawalkscomponents.schemas, infers Rust types, and builds a type dependency graph. -
Render templates — Askama templates produce
main.rs,registry.rs, handler modules, controller modules, andCargo.toml. -
Write project —
generator::projectcreates the directory structure and writes all generated files, then runsrustfmt.
Generated services use the same runtime (router, dispatcher, middleware, security, validator) as the library; they just plug in their own handlers and config.
-
HTTP server (
may_minihttp) accepts the connection and parses the request. - Middleware (pre) — CORS, tracing span, auth (security providers). Auth can return 401/403.
- Validation — Parameters and body validated against the spec; 400 with Problem Details on failure.
-
Router —
match_route(method, path)returns aRouteMatch(handler name + path/query params) or 404. -
Dispatcher — Sends a
HandlerRequestto the handler coroutine; waits forHandlerResponse. -
Handler — Runs in a
maycoroutine; business logic; returns status, headers, body. - Validation (response) — Optional response schema check; can log or fail.
- Middleware (post) — Metrics, tracing end, security headers.
- HTTP response — Written back to the client.
| Component | Path | Role |
|---|---|---|
| spec | src/spec/ |
Load OpenAPI (load.rs), build route list (build.rs), types (types.rs: RouteMeta, ParameterMeta, security, etc.). |
| router | src/router/ |
Build and run route matching. core.rs: Router, RouteMatch; radix.rs: radix tree for O(k) path match. Uses SmallVec for params (JSF hot path). |
| dispatcher | src/dispatcher/ |
HandlerRequest / HandlerResponse, handler lookup, channel send/recv, worker pool (worker_pool.rs). Dispatches to handler coroutines. |
| server | src/server/ |
AppService: wires router, dispatcher, security, middleware, static files, validator cache. service.rs implements the main request lifecycle; request.rs / response.rs for HTTP parsing/serialization. |
| middleware | src/middleware/ |
CORS (cors/), auth (auth.rs), metrics (metrics.rs), tracing (tracing.rs), memory (memory.rs). Chain runs before and after handler. |
| security | src/security/ |
SecurityProvider trait; implementations: API key, Bearer JWT, JWKS, OAuth2, remote API key. Used by auth middleware. |
| validator | src/validator.rs |
Request/response validation against OpenAPI schemas; RFC 7807 errors. validator_cache.rs caches compiled validators. |
| generator | src/generator/ |
Schema analysis (schema.rs), Askama rendering (templates.rs), project writing (project/). Used by brrtrouter-gen. |
| typed | src/typed/ |
Typed handler trait and TryFrom<HandlerRequest> for generated request types; integrates with dispatcher and codegen. |
| hot_reload | src/hot_reload.rs |
Watches spec file; rebuilds router and re-registers routes with the running server. |
| cli | src/cli/ |
CLI for the generator binary: generate, etc. |
| sse | src/sse.rs |
Server-Sent Events helpers (x-sse extension). |
| static_files | src/static_files.rs |
Serving static files and doc directory. |
The main binary (e.g. pet_store) loads the spec, builds the Router, creates AppService, registers handlers with the Dispatcher, and starts the HTTP server. The generator binary (brrtrouter-gen) uses spec + generator + cli to emit a new project.
The following diagram shows how the main runtime components relate. Code generation uses spec + generator (and cli); the running server uses the rest.
flowchart TB
subgraph client["Client"]
HTTP["HTTP Request"]
end
subgraph server_layer["Server (may_minihttp)"]
AppService["AppService\n(server/service.rs)"]
end
subgraph pipeline["Request pipeline"]
Middleware["Middleware Chain\n(CORS → Tracing → Auth → Metrics)"]
Validator["Validator\n(request/response schema)"]
Router["Router\n(router/core + radix)"]
Dispatcher["Dispatcher\n(dispatcher/core + worker_pool)"]
end
subgraph security_layer["Security"]
AuthMiddleware["AuthMiddleware"]
SecurityProviders["SecurityProviders\n(API Key, JWT, JWKS, OAuth2)"]
end
subgraph handlers["Handlers"]
HandlerRegistry["Handler Registry\n(name → coroutine)"]
HandlerA["Handler A\n(coroutine)"]
HandlerB["Handler B\n(coroutine)"]
end
subgraph spec_and_config["Design-time / config"]
OpenAPI["OpenAPI Spec"]
Config["config.yaml"]
end
HTTP --> AppService
AppService --> Middleware
Middleware --> AuthMiddleware
AuthMiddleware --> SecurityProviders
Middleware --> Validator
Validator --> Router
Router --> Dispatcher
Dispatcher --> HandlerRegistry
HandlerRegistry --> HandlerA
HandlerRegistry --> HandlerB
HandlerA --> Dispatcher
HandlerB --> Dispatcher
Dispatcher --> Validator
Validator --> Middleware
Middleware --> AppService
AppService --> HTTP
OpenAPI --> Router
OpenAPI --> Validator
OpenAPI --> AuthMiddleware
Config --> AppService
Config --> SecurityProviders
Data flow (simplified): Client → AppService → Middleware (CORS, tracing, auth) → Validator → Router (get RouteMatch) → Dispatcher → Handler coroutine → response back through Dispatcher → Validator → Middleware → AppService → Client. The OpenAPI spec drives Router, Validator, and auth registration; config.yaml drives server and security options.
Companion to the component graph: one request from the client to the handler and back.
sequenceDiagram
participant Client
participant Server as HttpServer
participant Middleware as Middleware Chain
participant Auth as AuthMiddleware
participant Validator as Validator
participant Router as Router
participant Dispatcher as Dispatcher
participant Handler as Handler (coroutine)
Client->>Server: HTTP Request (method, path, headers, body)
Server->>Middleware: pre: CORS, tracing span
Middleware->>Auth: validate credentials
alt auth required & invalid
Auth-->>Client: 401 or 403
end
Auth->>Validator: validate request (params, body schema)
alt validation failed
Validator-->>Client: 400 + RFC 7807
end
Validator->>Router: match_route(method, path)
alt no match
Router-->>Client: 404
end
Router->>Dispatcher: dispatch(handler_name, HandlerRequest)
Dispatcher->>Handler: send via channel
Note over Handler: business logic in coroutine
Handler->>Dispatcher: HandlerResponse
Dispatcher->>Validator: validate response (optional)
Validator->>Middleware: post: metrics, tracing end
Middleware->>Server: response
Server->>Client: HTTP Response
Together, the component graph (section 5) and this sequence diagram (section 6) describe how the code is structured and how a single request moves through it.
- ARCHITECTURE.md — Detailed architecture, code-generation and request-handling flows, observability.
- RequestLifecycle.md — End-to-end request lifecycle and code generation.
- README.md — Features, quick start, benchmarks.
- DEVELOPMENT.md — Development workflow and just commands.
Footer
Sidebar