pub struct Router { /* private fields */ }Expand description
HTTP request router with path-parameter extraction and method-based dispatch.
Implementations§
Source§impl Router
impl Router
Sourcepub fn with_state<T: Clone + Send + Sync + 'static>(self, state: T) -> Self
pub fn with_state<T: Clone + Send + Sync + 'static>(self, state: T) -> Self
Attach application state of type T to this router.
The state is wrapped in an Arc<T> and injected into every request’s
extensions map just before the handler is invoked. Handlers retrieve
it with req.state::<T>().
Nested routers that do not have their own state automatically inherit this router’s state during dispatch.
#[derive(Clone)]
struct AppState { db_url: String }
let state = AppState { db_url: "postgres://localhost/mydb".into() };
let router = Router::new()
.with_state(state)
.get("/", |req: Request| async move {
let s = req.state::<AppState>().expect("state present");
oxihttp_server::response::text_response(&s.db_url)
});Sourcepub fn route<F, Fut>(self, method: Method, path: &str, handler: F) -> Self
pub fn route<F, Fut>(self, method: Method, path: &str, handler: F) -> Self
Register a route for the given method and path pattern.
Path patterns support:
- Literal segments:
/users/list - Parameters:
/users/:id - Wildcards:
/static/*path
Sourcepub fn nest(self, prefix: &str, router: Router) -> Self
pub fn nest(self, prefix: &str, router: Router) -> Self
Nest a sub-router under the given prefix.
Sourcepub fn host(self, host: &str, router: Router) -> Self
pub fn host(self, host: &str, router: Router) -> Self
Route requests with the given Host header value to router.
The host value is matched case-insensitively against the bare hostname
(port suffix stripped). When a match is found the request is forwarded
to router without any path rewriting. Virtual-host dispatch happens
before nested-prefix dispatch.
§Example
let api = Router::new().get("/v1", |_req| async {
oxihttp_server::response::text_response("api")
});
let web = Router::new().get("/", |_req| async {
oxihttp_server::response::text_response("web")
});
let router = Router::new()
.host("api.example.com", api)
.host("example.com", web);Sourcepub fn fallback<F, Fut>(self, handler: F) -> Self
pub fn fallback<F, Fut>(self, handler: F) -> Self
Set a fallback handler for routes that don’t match (custom 404).
Sourcepub fn method_not_allowed<F, Fut>(self, handler: F) -> Self
pub fn method_not_allowed<F, Fut>(self, handler: F) -> Self
Set a handler for method-not-allowed (405) responses.
Sourcepub fn resolve(
&self,
method: &Method,
path: &str,
) -> Option<HashMap<String, String>>
pub fn resolve( &self, method: &Method, path: &str, ) -> Option<HashMap<String, String>>
Match a request path against registered routes without dispatching.
Replicates the O(n) dispatch scan for use in benchmarks and introspection.
Returns extracted path parameters on a successful match, None on no match.
When the path is found but the method is not registered the method returns
Some(HashMap::new()) — an empty map — to signal a 405 situation without
actually dispatching.
Sourcepub fn dispatch(&self, req: Request<Incoming>) -> DispatchFuture<'_>
pub fn dispatch(&self, req: Request<Incoming>) -> DispatchFuture<'_>
Dispatch an incoming request through the router.
Sourcepub fn route_count(&self) -> usize
pub fn route_count(&self) -> usize
Return the number of registered routes (not including nested).