BloomApiRouter class

High-performance server router for Bloom API routes, WebSocket gateways, and SSR endpoints.

Architectural Contract

  • Provides unified route registration (get, post, put, delete, patch, all) with nested or route-scoped middleware pipelines and global pipeline hooks (use).
  • Supports path parameter matching (/api/tasks/:id), wildcard captures (/*), and automatic OpenAPI 3.1 schema specification generation and interactive Scalar / Swagger UI mounting (enableOpenApi).
  • Bridges native dart:io HttpRequest to typed, testable BloomRequest and BloomResponse abstractions.

Concurrency & Shutdown Model

  • Non-blocking asynchronous request handling across server isolates.
  • Tracks active requests via in-flight request completers to support graceful zero-downtime shutdown via close, draining active connections within a configurable timeout before force-closing sockets.
  • Rejects incoming requests with HTTP 503 Service Unavailable during shutdown drain phase.

Specificity-Based Route Sorting

Routes are sorted by specificity score (exact static matches > parameterized segments > wildcard routes) so registration order does not cause unintentional route shadowing.

Streaming Request Guards & Response Backpressure

  • handleIoRequest checks byte stream length against maxRequestBodyBytes before buffering full payloads, short-circuiting oversized payload attacks with HTTP 413 Payload Too Large.
  • Incremental responses use addStream to propagate backpressure from the client socket directly to the byte producer.

Example

final router = BloomApiRouter();

// Global middleware
router.use(BloomCorsMiddleware());

// Route definitions
router.get('/api/health', (req) async => BloomResponse.json({'status': 'ok'}));
router.get('/api/users/:id', (req) async {
  final id = req.params['id']!;
  return BloomResponse.json({'id': id, 'name': 'User $id'});
});

// Bind and listen
final server = await router.serve(port: 8080);

Constructors

BloomApiRouter()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

all(String path, BloomRouteHandler handler, {List<BloomMiddleware> middlewares = const []}) → void
Registers a route for path matching all HTTP methods (GET, POST, PUT, DELETE, PATCH, OPTIONS, etc.).
close({Duration gracePeriod = const Duration(seconds: 30)}) Future<void>
Gracefully closes all active HttpServer instances and drains in-flight requests.
delete(String path, BloomRouteHandler handler, {List<BloomMiddleware> middlewares = const []}) → void
Registers a DELETE route for path handled by handler with optional route-scoped middlewares.
enableOpenApi({String title = 'Bloom API', String version = '1.0.0', String description = 'Full-stack Bloom Server API', String schemaPath = '/api/openapi.json', String docsPath = '/api/docs', String swaggerPath = '/api/swagger'}) → void
Automatically generates an OpenAPI 3.1 specification and mounts interactive Scalar and Swagger UI documentation endpoints.
get(String path, BloomRouteHandler handler, {List<BloomMiddleware> middlewares = const []}) → void
Registers a GET route for path handled by handler with optional route-scoped middlewares.
group(String prefix, void configure(BloomRouteGroup group), {List<BloomMiddleware> middlewares = const []}) → void
Creates a scoped route group with a URL prefix and optional inherited middlewares.
handle(BloomRequest request) Future<BloomResponse>
Dispatches and processes an incoming BloomRequest through the middleware and route pipeline.
handleIoRequest(HttpRequest ioReq, {int? maxRequestBodyBytes}) Future<void>
Bridges a native dart:io HttpRequest to the Bloom router pipeline.
handleRequest(BloomRequest request) Future<BloomResponse>
Dispatches and processes an incoming BloomRequest through global middlewares and matching routes.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
options(String path, BloomRouteHandler handler, {List<BloomMiddleware> middlewares = const []}) → void
Registers an OPTIONS route for path handled by handler with optional route-scoped middlewares.
patch(String path, BloomRouteHandler handler, {List<BloomMiddleware> middlewares = const []}) → void
Registers a PATCH route for path handled by handler with optional route-scoped middlewares.
post(String path, BloomRouteHandler handler, {List<BloomMiddleware> middlewares = const []}) → void
Registers a POST route for path handled by handler with optional route-scoped middlewares.
put(String path, BloomRouteHandler handler, {List<BloomMiddleware> middlewares = const []}) → void
Registers a PUT route for path handled by handler with optional route-scoped middlewares.
serve({InternetAddress? address, int port = 8080, SecurityContext? securityContext, int? maxRequestBodyBytes}) Future<HttpServer>
Binds to a native dart:io HttpServer and begins serving API and SSR requests.
ssr(String path, BloomNode builder(BloomRequest request), {HeadManager head(BloomRequest request)?, String layout(String bodyHtml, HeadManager? head)?, List<BloomMiddleware> middlewares = const []}) → void
Mounts a high-performance Server-Side Rendered (SSR) endpoint using BloomNode.
toOpenApiSpec({String title = 'Bloom API', String version = '1.0.0', String description = 'Full-stack Bloom Server API'}) Map<String, dynamic>
Generates an OpenAPI 3.1 specification map from all registered routes.
toString() String
A string representation of this object.
inherited
use(BloomMiddleware middleware) → void
Registers a global middleware executed before all route handlers in this router.

Operators

operator ==(Object other) bool
The equality operator.
inherited