pub enum ProductOSRouter<S = ()> {
Axum(AxumRouter<S>),
}Expand description
Main router enum for Product OS Router
Dispatches to either Axum or Flow implementation based on feature flags. 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, 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);Variants§
Axum(AxumRouter<S>)
Implementations§
Source§impl<S> ProductOSRouter<S>
impl<S> ProductOSRouter<S>
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.
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
Sourcepub fn not_implemented() -> Response<Body>
pub fn not_implemented() -> Response<Body>
Returns a 501 Not Implemented JSON response
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>
Use not_implemented() which correctly returns 501, or build a 404 response explicitly
Returns a 404 Not Found JSON response
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
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
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 without state
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 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 without state
Source§impl<S> ProductOSRouter<S>
impl<S> ProductOSRouter<S>
Sourcepub fn get_router(&self) -> ProductOSRouter<S>
pub fn get_router(&self) -> ProductOSRouter<S>
Return a clone of the router.
For both Axum and Flow, returns ProductOSRouter<S>.
Use into_axum_router() when you need the raw axum::Router.
Sourcepub fn into_axum_router(self) -> Router
pub fn into_axum_router(self) -> Router
Extract the inner axum::Router, consuming the wrapper.
This is needed when handing the router to axum-specific server functions
that expect a raw axum::Router.
Sourcepub fn add_handler<H, T>(&mut self, path: &str, method: Method, handler: H)
pub fn add_handler<H, T>(&mut self, path: &str, method: Method, handler: H)
Add a handler for the given HTTP method and path
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
Sourcepub fn add_handlers<H, T>(&mut self, path: &str, handlers: BTreeMap<Method, H>)
pub fn add_handlers<H, T>(&mut self, path: &str, handlers: BTreeMap<Method, H>)
Add multiple HTTP method handlers at a single path
Sourcepub fn add_route(&mut self, path: &str, method_router: MethodRouter<S>)
pub fn add_route(&mut self, path: &str, method_router: MethodRouter<S>)
Add a route with an Axum MethodRouter.
Sourcepub fn set_fallback(&mut self, service_handler: MethodRouter<S>)
pub fn set_fallback(&mut self, service_handler: MethodRouter<S>)
Set the fallback service for unmatched routes (Axum MethodRouter).
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 the fallback handler for unmatched routes.
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,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Box<dyn Error + Send + Sync>>,
ResBody: Body<Data = Bytes> + Send + 'static,
<ResBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>,
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,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send,
<<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Box<dyn Error + Send + Sync>>,
ResBody: Body<Data = Bytes> + Send + 'static,
<ResBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>,
Add an Axum-compatible Tower middleware layer.
Trait Implementations§
Source§impl<S> Clone for ProductOSRouter<S>
Available on crate feature framework_axum only.
impl<S> Clone for ProductOSRouter<S>
framework_axum only.Source§fn clone(&self) -> ProductOSRouter<S>
fn clone(&self) -> ProductOSRouter<S>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Default for ProductOSRouter
Available on crate features framework_axum or framework_flow only.
impl Default for ProductOSRouter
framework_axum or framework_flow only.