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:ioHttpRequest 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
maxRequestBodyBytesbefore buffering full payloads, short-circuiting oversized payload attacks with HTTP 413 Payload Too Large. - Incremental responses use
addStreamto 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
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
pathmatching 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
DELETEroute forpathhandled byhandlerwith optional route-scopedmiddlewares. -
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
GETroute forpathhandled byhandlerwith optional route-scopedmiddlewares. -
group(
String prefix, void configure(BloomRouteGroup group), {List< BloomMiddleware> middlewares = const []}) → void -
Creates a scoped route group with a URL
prefixand optional inheritedmiddlewares. -
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:ioHttpRequest 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
OPTIONSroute forpathhandled byhandlerwith optional route-scopedmiddlewares. -
patch(
String path, BloomRouteHandler handler, {List< BloomMiddleware> middlewares = const []}) → void -
Registers a
PATCHroute forpathhandled byhandlerwith optional route-scopedmiddlewares. -
post(
String path, BloomRouteHandler handler, {List< BloomMiddleware> middlewares = const []}) → void -
Registers a
POSTroute forpathhandled byhandlerwith optional route-scopedmiddlewares. -
put(
String path, BloomRouteHandler handler, {List< BloomMiddleware> middlewares = const []}) → void -
Registers a
PUTroute forpathhandled byhandlerwith optional route-scopedmiddlewares. -
serve(
{InternetAddress? address, int port = 8080, SecurityContext? securityContext, int? maxRequestBodyBytes}) → Future< HttpServer> -
Binds to a native
dart:ioHttpServer 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
middlewareexecuted before all route handlers in this router.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited