Router

Struct Router 

Source
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

Source

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.

Source

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.

Source

pub fn openapi_json_with_description( &self, title: &str, version: &str, description: &str, ) -> String

Generate OpenAPI JSON with description

Source

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

Source

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

pub fn to_openapi_with_description( &self, title: &str, version: &str, description: &str, ) -> Value

Generate OpenAPI 3.1 specification with description

Source§

impl Router

Source

pub fn new() -> Self

Create a new router

Source

pub fn with_config(config: RouterConfig) -> Self

Create a new router with configuration

Source

pub fn register<F, Fut>(&mut self, name: &str, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = String> + Send + 'static,

Register a handler with a name

Source

pub fn handlers_count(&self) -> usize

Get the number of registered handlers

Source

pub fn add_adapter(&mut self, adapter: Box<dyn ProtocolAdapter>)

Add a protocol adapter

Source

pub fn has_adapter(&self, name: &str) -> bool

Check if an adapter is registered

Source

pub fn get_adapter(&self, name: &str) -> Option<&dyn ProtocolAdapter>

Get an adapter by name

Source

pub async fn route_request( &self, protocol: &str, request: &str, ) -> Result<String, String>

Route a request through the appropriate protocol adapter

Source

pub async fn execute(&self, name: &str) -> Result<String, String>

Execute a handler by name

Source

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.

Source

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.

Source

pub fn can_handle_rest(&self, _name: &str) -> bool

Check if handler can be called via REST

Source

pub fn can_handle_graphql(&self, _name: &str) -> bool

Check if handler can be called via GraphQL

Source

pub fn can_handle_grpc(&self, _name: &str) -> bool

Check if handler can be called via gRPC

Source

pub fn enabled_protocols(&self) -> Vec<String>

Get list of enabled protocols

Source

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).

Source

pub fn routes(&self) -> &[RouteMetadata]

Get all registered routes

Returns an immutable reference to all route metadata. This is used for documentation generation.

Source

pub fn get<F, Fut>(&mut self, path: &str, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = String> + Send + 'static,

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}”.

Source

pub fn post<F, Fut>(&mut self, path: &str, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = String> + Send + 'static,

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}”.

Source

pub fn put<F, Fut>(&mut self, path: &str, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = String> + Send + 'static,

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}”.

Source

pub fn delete<F, Fut>(&mut self, path: &str, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = String> + Send + 'static,

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}”.

Source

pub fn patch<F, Fut>(&mut self, path: &str, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = String> + Send + 'static,

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}”.

Source

pub fn head<F, Fut>(&mut self, path: &str, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = String> + Send + 'static,

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}”.

Source

pub fn options<F, Fut>(&mut self, path: &str, handler: F)
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = String> + Send + 'static,

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}”.

Source

pub async fn call_rest( &self, method: &str, path: &str, ) -> Result<String, String>

Call handler via REST

Source

pub async fn call_graphql(&self, query: &str) -> Result<String, String>

Call handler via GraphQL

Source

pub async fn call_grpc( &self, method: &str, request: &str, ) -> Result<String, String>

Call handler via gRPC

Source

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 title
  • version - 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 endpoint
Source

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 configuration
  • title - API title
  • version - 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

Source§

fn generate_contract_tests(&self) -> ContractTestResults

Generate contract tests for all routes
Source§

fn test_route_contract(&self, path: &str, method: &str) -> ContractTestResult

Test a specific route path and method
Source§

impl Default for Router

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more