pub struct ProductOSRouter<S = ()> { /* private fields */ }Expand description
Main router struct for Product OS Router
Provides a high-level API for defining routes, handlers, and middleware. Supports both stateful and stateless routing.
§Type Parameters
S- The state type (defaults to()for stateless routing)
§Examples
§Stateless Router
use product_os_router::ProductOSRouter;
async fn handler() -> &'static str {
"Hello!"
}
let mut router = ProductOSRouter::new();
router.add_get_no_state("/", handler);§Stateful Router
use product_os_router::ProductOSRouter;
use axum::extract::State;
#[derive(Clone)]
struct AppState {
message: String,
}
async fn handler(State(state): State<AppState>) -> String {
state.message
}
let state = AppState { message: "Hello!".to_string() };
let mut router = ProductOSRouter::new_with_state(state);
router.add_get("/", handler);Implementations§
Source§impl ProductOSRouter
impl ProductOSRouter
Sourcepub fn new() -> ProductOSRouter
pub fn new() -> ProductOSRouter
Create a new stateless router
Creates a router without any shared state. Use new_with_state
if you need to share state between handlers.
§Examples
use product_os_router::ProductOSRouter;
let router = ProductOSRouter::new();Source§impl ProductOSRouter
impl ProductOSRouter
Sourcepub fn param_to_field(path: &str) -> String
pub fn param_to_field(path: &str) -> String
Convert path parameters from Express-style to Axum format
Transforms route paths with :param and *wildcard syntax into Axum’s {param} format.
§Arguments
path- The path with Express-style parameters
§Returns
The path converted to Axum format
§Examples
use product_os_router::ProductOSRouter;
assert_eq!(ProductOSRouter::param_to_field("/users/:id"), "/users/{id}");
assert_eq!(ProductOSRouter::param_to_field("/files/*path"), "/files/{path}");Sourcepub fn add_service_no_state<Z>(&mut self, path: &str, service: Z)
pub fn add_service_no_state<Z>(&mut self, path: &str, service: Z)
Add a Tower service as a route handler without state.
The service must accept Request<Body> and return Response<Body>.
Sourcepub fn add_middleware_no_state<L, ResBody>(&mut self, middleware: L)where
L: Layer<Route> + Clone + Send + Sync + 'static,
<L as Layer<Route>>::Service: Service<Request<Body>, Response = Response<ResBody>> + Clone + Send + Sync + 'static,
ResBody: Body<Data = Bytes> + Send + 'static,
<ResBody as Body>::Error: Into<Box<dyn Error + Sync + Send>>,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Box<dyn Error + Sync + Send>> + 'static,
pub fn add_middleware_no_state<L, ResBody>(&mut self, middleware: L)where
L: Layer<Route> + Clone + Send + Sync + 'static,
<L as Layer<Route>>::Service: Service<Request<Body>, Response = Response<ResBody>> + Clone + Send + Sync + 'static,
ResBody: Body<Data = Bytes> + Send + 'static,
<ResBody as Body>::Error: Into<Box<dyn Error + Sync + Send>>,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Box<dyn Error + Sync + Send>> + 'static,
Add a Tower middleware layer to the stateless router.
The layer wraps every route and is applied in LIFO order (last added runs first).
Sourcepub fn add_default_header_no_state(
&mut self,
header_name: &str,
header_value: &str,
)
pub fn add_default_header_no_state( &mut self, header_name: &str, header_value: &str, )
Add a default header to all responses from this stateless router.
Headers added this way will not overwrite headers already present in the response.
Sourcepub fn not_implemented() -> Response<Body>
pub fn not_implemented() -> Response<Body>
Returns a 501 Not Implemented JSON response.
Use this to indicate that the server does not support the functionality required to fulfill the request.
Sourcepub fn not_implemented_legacy() -> Response<Body>
👎Deprecated since 0.0.41: Use not_implemented() which correctly returns 501, or build a 404 response explicitly
pub fn not_implemented_legacy() -> Response<Body>
Returns a 404 Not Found JSON response.
This was the original behavior of not_implemented() prior to v0.0.41.
Use not_implemented for the correct 501 status code,
or use this method if you specifically need a 404 response.
Sourcepub fn add_route_no_state(&mut self, path: &str, service_handler: MethodRouter)
pub fn add_route_no_state(&mut self, path: &str, service_handler: MethodRouter)
Add a pre-built MethodRouter at the given path without state.
Sourcepub fn set_fallback_no_state(&mut self, service_handler: MethodRouter)
pub fn set_fallback_no_state(&mut self, service_handler: MethodRouter)
Set a fallback MethodRouter for unmatched routes without state.
Sourcepub fn add_get_no_state<H, T>(&mut self, path: &str, handler: H)
pub fn add_get_no_state<H, T>(&mut self, path: &str, handler: H)
Add a GET route handler without state
§Arguments
path- The route path (supports:paramand*wildcard)handler- The async function to handle requests
§Examples
use product_os_router::ProductOSRouter;
async fn hello() -> &'static str {
"Hello, World!"
}
let mut router = ProductOSRouter::new();
router.add_get_no_state("/", hello);Sourcepub fn add_post_no_state<H, T>(&mut self, path: &str, handler: H)
pub fn add_post_no_state<H, T>(&mut self, path: &str, handler: H)
Add a POST route handler without state
§Arguments
path- The route pathhandler- The async function to handle requests
§Examples
use product_os_router::{ProductOSRouter, Json};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct User {
name: String,
}
async fn create_user(Json(user): Json<User>) -> Json<User> {
Json(user)
}
let mut router = ProductOSRouter::new();
router.add_post_no_state("/users", create_user);Sourcepub fn add_handler_no_state<H, T>(
&mut self,
path: &str,
method: Method,
handler: H,
)
pub fn add_handler_no_state<H, T>( &mut self, path: &str, method: Method, handler: H, )
Add a handler for the given HTTP method and path without state.
Sourcepub fn set_fallback_handler_no_state<H, T>(&mut self, handler: H)
pub fn set_fallback_handler_no_state<H, T>(&mut self, handler: H)
Set a fallback handler for unmatched routes without state.
Source§impl<S> ProductOSRouter<S>
impl<S> ProductOSRouter<S>
Sourcepub fn get_router(&self) -> Router
pub fn get_router(&self) -> Router
Build and return the finalized Router with the stored state applied.
The returned Router can be used with a server (e.g., axum::serve).
Sourcepub fn get_router_mut(&mut self) -> &mut Router<S>
pub fn get_router_mut(&mut self) -> &mut Router<S>
Return a mutable reference to the underlying Router for advanced configuration.
Sourcepub fn new_with_state(state: S) -> ProductOSRouter<S>
pub fn new_with_state(state: S) -> ProductOSRouter<S>
Create a new router with the given shared state.
The state is made available to handlers via the State extractor.
Sourcepub fn add_service<Z>(&mut self, path: &str, service: Z)
pub fn add_service<Z>(&mut self, path: &str, service: Z)
Add a Tower service as a route handler with state.
Sourcepub fn add_middleware<L, ResBody>(&mut self, middleware: L)where
L: Layer<Route> + Clone + Send + Sync + 'static,
<L as Layer<Route>>::Service: Service<Request<Body>, Response = Response<ResBody>> + Clone + Send + Sync + 'static,
ResBody: Body<Data = Bytes> + Send + 'static,
<ResBody as Body>::Error: Into<Box<dyn Error + Sync + Send>>,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Box<dyn Error + Sync + Send>> + 'static,
pub fn add_middleware<L, ResBody>(&mut self, middleware: L)where
L: Layer<Route> + Clone + Send + Sync + 'static,
<L as Layer<Route>>::Service: Service<Request<Body>, Response = Response<ResBody>> + Clone + Send + Sync + 'static,
ResBody: Body<Data = Bytes> + Send + 'static,
<ResBody as Body>::Error: Into<Box<dyn Error + Sync + Send>>,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Box<dyn Error + Sync + Send>> + 'static,
Add a Tower middleware layer to the router.
Sourcepub fn add_default_header(&mut self, header_name: &str, header_value: &str)
pub fn add_default_header(&mut self, header_name: &str, header_value: &str)
Add a default header to all responses from this router.
Headers added this way will not overwrite headers already present in the response.
§Arguments
header_name- The header nameheader_value- The header value
Sourcepub fn add_default_header_owned(
&mut self,
header_name: String,
header_value: String,
)
👎Deprecated since 0.0.41: Use add_default_header(&str, &str) instead
pub fn add_default_header_owned( &mut self, header_name: String, header_value: String, )
Add a default header to all responses from this router.
This method accepts owned String parameters for backward compatibility.
Prefer add_default_header which takes &str instead.
Sourcepub fn add_route(&mut self, path: &str, service_handler: MethodRouter<S>)
pub fn add_route(&mut self, path: &str, service_handler: MethodRouter<S>)
Add a pre-built MethodRouter at the given path.
Sourcepub fn set_fallback(&mut self, service_handler: MethodRouter<S>)
pub fn set_fallback(&mut self, service_handler: MethodRouter<S>)
Set a fallback MethodRouter for unmatched routes.
Sourcepub fn add_get<H, T>(&mut self, path: &str, handler: H)where
H: Handler<T, S>,
T: 'static,
pub fn add_get<H, T>(&mut self, path: &str, handler: H)where
H: Handler<T, S>,
T: 'static,
Add a GET route handler with state.
Sourcepub fn add_post<H, T>(&mut self, path: &str, handler: H)where
H: Handler<T, S>,
T: 'static,
pub fn add_post<H, T>(&mut self, path: &str, handler: H)where
H: Handler<T, S>,
T: 'static,
Add a POST route handler with state.
Sourcepub fn add_handler<H, T>(&mut self, path: &str, method: Method, handler: H)where
H: Handler<T, S>,
T: 'static,
pub fn add_handler<H, T>(&mut self, path: &str, method: Method, handler: H)where
H: Handler<T, S>,
T: 'static,
Add a handler for the given HTTP method and path with state.
Sourcepub fn set_fallback_handler<H, T>(&mut self, handler: H)where
H: Handler<T, S>,
T: 'static,
pub fn set_fallback_handler<H, T>(&mut self, handler: H)where
H: Handler<T, S>,
T: 'static,
Set a fallback handler for unmatched routes with state.