pub struct APIRouter<S = ()> {
pub prefix: String,
pub routes: Vec<Route<S>>,
}Expand description
A router for defining and organizing API routes.
APIRouter provides a fluent interface for registering HTTP route handlers
with automatic OpenAPI documentation generation. Routes can be organized
using path prefixes and routers can be combined using include_router().
§Type Parameters
S- The application state type
§Examples
use fastrust::{APIRouter, RouteConfig};
use axum::extract::Path;
async fn hello(Path(name): Path<String>) -> String {
format!("Hello {}", name)
}
let mut api = APIRouter::new("/api");
api.get("/hello/{name}", hello, RouteConfig::default().summary("Say hello"));§Path Prefixes
Routes registered with an APIRouter automatically have the router’s
prefix prepended. For example, a router with prefix /api and a route
/users will result in the full path /api/users.
§Combining Routers
Routers can be combined using include_router():
use fastrust::APIRouter;
let mut api = APIRouter::new("/api");
let mut v1 = APIRouter::new("/v1");
v1.include_router(api); // Routes become /v1/api/...Fields§
§prefix: StringThe path prefix for all routes in this router.
routes: Vec<Route<S>>The list of routes registered in this router.
Implementations§
Source§impl<S> APIRouter<S>
impl<S> APIRouter<S>
Sourcepub fn include_router(&mut self, router: APIRouter<S>)
pub fn include_router(&mut self, router: APIRouter<S>)
Includes all routes from another router into this one.
This method consumes the provided router and adds all its routes to this router, prepending this router’s prefix to each route’s path.
§Arguments
router- The router to include
§Examples
use fastrust::APIRouter;
let mut api = APIRouter::new("/api");
let mut v1 = APIRouter::new("/v1");
v1.include_router(api); // api routes now have /v1 prefix