pub struct Router { /* private fields */ }Expand description
Router manages handler registration and protocol adapters
The router allows you to register handlers once and expose them via multiple protocols based on configuration.
Implementations§
Source§impl Router
impl Router
Sourcepub fn docs_config(&self, path: &str, title: &str, version: &str) -> DocsConfig
pub fn docs_config(&self, path: &str, title: &str, version: &str) -> DocsConfig
Get documentation configuration helper
Returns a DocsConfig that can be used to serve documentation.
Sourcepub fn openapi_json(&self, title: &str, version: &str) -> String
pub fn openapi_json(&self, title: &str, version: &str) -> String
Generate OpenAPI JSON for serving at /docs/openapi.json
This is a convenience method that generates the OpenAPI spec in JSON format ready to be served via HTTP.
Sourcepub fn openapi_json_with_description(
&self,
title: &str,
version: &str,
description: &str,
) -> String
pub fn openapi_json_with_description( &self, title: &str, version: &str, description: &str, ) -> String
Generate OpenAPI JSON with description
Sourcepub fn docs_html(&self, config: &DocsConfig) -> String
pub fn docs_html(&self, config: &DocsConfig) -> String
Generate a basic HTML page for documentation
Returns a simple HTML page that can serve as a landing page for API documentation. In production, you’d want to use a proper documentation UI like Scalar or Swagger UI.
Source§impl Router
impl Router
Sourcepub fn to_openapi(&self, title: &str, version: &str) -> Value
pub fn to_openapi(&self, title: &str, version: &str) -> Value
Generate OpenAPI 3.1 specification
This is a convenience method that creates an OpenAPI specification for all REST routes registered with this router.
Source§impl Router
impl Router
Sourcepub fn with_config(config: RouterConfig) -> Self
Available on crate feature router only.
pub fn with_config(config: RouterConfig) -> Self
router only.Create a new router with configuration
Sourcepub fn handlers_count(&self) -> usize
pub fn handlers_count(&self) -> usize
Get the number of registered handlers
Sourcepub fn add_adapter(&mut self, adapter: Box<dyn ProtocolAdapter>)
pub fn add_adapter(&mut self, adapter: Box<dyn ProtocolAdapter>)
Add a protocol adapter
Sourcepub fn has_adapter(&self, name: &str) -> bool
pub fn has_adapter(&self, name: &str) -> bool
Check if an adapter is registered
Sourcepub fn get_adapter(&self, name: &str) -> Option<&dyn ProtocolAdapter>
pub fn get_adapter(&self, name: &str) -> Option<&dyn ProtocolAdapter>
Get an adapter by name
Sourcepub async fn route_request(
&self,
protocol: &str,
request: &str,
) -> Result<String, String>
pub async fn route_request( &self, protocol: &str, request: &str, ) -> Result<String, String>
Route a request through the appropriate protocol adapter
Sourcepub fn list_handlers(&self) -> Vec<String>
pub fn list_handlers(&self) -> Vec<String>
List all registered handler names
Returns a vector of all handler names that have been registered with this router. Used by MCP server for tool discovery.
Sourcepub async fn call_handler(
&self,
name: &str,
_request: &str,
) -> Result<String, String>
pub async fn call_handler( &self, name: &str, _request: &str, ) -> Result<String, String>
Call a handler by name with request data
This is an alias for execute() that provides a more explicit
API for directly calling handlers. Used by MCP server.
Sourcepub fn can_handle_rest(&self, _name: &str) -> bool
pub fn can_handle_rest(&self, _name: &str) -> bool
Check if handler can be called via REST
Sourcepub fn can_handle_graphql(&self, _name: &str) -> bool
pub fn can_handle_graphql(&self, _name: &str) -> bool
Check if handler can be called via GraphQL
Sourcepub fn can_handle_grpc(&self, _name: &str) -> bool
pub fn can_handle_grpc(&self, _name: &str) -> bool
Check if handler can be called via gRPC
Sourcepub fn enabled_protocols(&self) -> Vec<String>
pub fn enabled_protocols(&self) -> Vec<String>
Get list of enabled protocols
Sourcepub fn add_route(&mut self, metadata: RouteMetadata)
pub fn add_route(&mut self, metadata: RouteMetadata)
Add a route with metadata
This stores route metadata that can be used to generate documentation (OpenAPI, GraphQL schemas, gRPC reflection).
Sourcepub fn routes(&self) -> &[RouteMetadata]
pub fn routes(&self) -> &[RouteMetadata]
Get all registered routes
Returns an immutable reference to all route metadata. This is used for documentation generation.
Sourcepub fn get<F, Fut>(&mut self, path: &str, handler: F)
pub fn get<F, Fut>(&mut self, path: &str, handler: F)
Register a GET route
This is a convenience method that registers both a handler and route metadata for a GET request. The handler name is automatically generated as “GET:{path}”.
Sourcepub fn post<F, Fut>(&mut self, path: &str, handler: F)
pub fn post<F, Fut>(&mut self, path: &str, handler: F)
Register a POST route
This is a convenience method that registers both a handler and route metadata for a POST request. The handler name is automatically generated as “POST:{path}”.
Sourcepub fn put<F, Fut>(&mut self, path: &str, handler: F)
pub fn put<F, Fut>(&mut self, path: &str, handler: F)
Register a PUT route
This is a convenience method that registers both a handler and route metadata for a PUT request. The handler name is automatically generated as “PUT:{path}”.
Sourcepub fn delete<F, Fut>(&mut self, path: &str, handler: F)
pub fn delete<F, Fut>(&mut self, path: &str, handler: F)
Register a DELETE route
This is a convenience method that registers both a handler and route metadata for a DELETE request. The handler name is automatically generated as “DELETE:{path}”.
Sourcepub fn patch<F, Fut>(&mut self, path: &str, handler: F)
pub fn patch<F, Fut>(&mut self, path: &str, handler: F)
Register a PATCH route
This is a convenience method that registers both a handler and route metadata for a PATCH request. The handler name is automatically generated as “PATCH:{path}”.
Sourcepub fn head<F, Fut>(&mut self, path: &str, handler: F)
pub fn head<F, Fut>(&mut self, path: &str, handler: F)
Register a HEAD route
This is a convenience method that registers both a handler and route metadata for a HEAD request. The handler name is automatically generated as “HEAD:{path}”.
Sourcepub fn options<F, Fut>(&mut self, path: &str, handler: F)
pub fn options<F, Fut>(&mut self, path: &str, handler: F)
Register an OPTIONS route
This is a convenience method that registers both a handler and route metadata for an OPTIONS request. The handler name is automatically generated as “OPTIONS:{path}”.
Sourcepub async fn call_rest(
&self,
method: &str,
path: &str,
) -> Result<String, String>
pub async fn call_rest( &self, method: &str, path: &str, ) -> Result<String, String>
Call handler via REST
Sourcepub async fn call_graphql(&self, query: &str) -> Result<String, String>
pub async fn call_graphql(&self, query: &str) -> Result<String, String>
Call handler via GraphQL
Sourcepub async fn call_grpc(
&self,
method: &str,
request: &str,
) -> Result<String, String>
pub async fn call_grpc( &self, method: &str, request: &str, ) -> Result<String, String>
Call handler via gRPC
Sourcepub fn scalar(&self, title: &str, version: &str) -> String
pub fn scalar(&self, title: &str, version: &str) -> String
Generate Scalar documentation HTML with default configuration
This is a convenience method that generates a complete HTML page with Scalar UI for interactive API documentation.
§Arguments
title- API titleversion- API version
§Example
use allframe_core::router::Router;
let mut router = Router::new();
router.get("/users", || async { "Users".to_string() });
let html = router.scalar("My API", "1.0.0");
// Serve this HTML at /docs endpointSourcepub fn scalar_docs(
&self,
config: ScalarConfig,
title: &str,
version: &str,
) -> String
pub fn scalar_docs( &self, config: ScalarConfig, title: &str, version: &str, ) -> String
Generate Scalar documentation HTML with custom configuration
This method allows full customization of the Scalar UI appearance and behavior.
§Arguments
config- Scalar configurationtitle- API titleversion- API version
§Example
use allframe_core::router::{Router, ScalarConfig, ScalarTheme};
let mut router = Router::new();
router.get("/users", || async { "Users".to_string() });
let config = ScalarConfig::new()
.theme(ScalarTheme::Light)
.show_sidebar(false);
let html = router.scalar_docs(config, "My API", "1.0.0");Trait Implementations§
Source§impl ContractTestable for Router
impl ContractTestable for Router
Source§fn generate_contract_tests(&self) -> ContractTestResults
fn generate_contract_tests(&self) -> ContractTestResults
Source§fn test_route_contract(&self, path: &str, method: &str) -> ContractTestResult
fn test_route_contract(&self, path: &str, method: &str) -> ContractTestResult
Auto Trait Implementations§
impl Freeze for Router
impl !RefUnwindSafe for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl !UnwindSafe for Router
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request