pub struct Router { /* private fields */ }Implementations§
Source§impl Router
impl Router
pub fn new() -> Self
Sourcepub fn with_state<T: Clone + Send + Sync + 'static>(&mut self, state: T)
pub fn with_state<T: Clone + Send + Sync + 'static>(&mut self, state: T)
Add a shared state to the router that will be available in all handlers
pub fn get<H, Args>(&mut self, path: &str, handler: H)
pub fn post<H, Args>(&mut self, path: &str, handler: H)
pub fn put<H, Args>(&mut self, path: &str, handler: H)
pub fn delete<H, Args>(&mut self, path: &str, handler: H)
pub fn patch<H, Args>(&mut self, path: &str, handler: H)
Sourcepub fn layer<L>(self, layer: L) -> Self
pub fn layer<L>(self, layer: L) -> Self
Add a middleware layer to all routes in the router.
The layer must implement tower::Layer<EndpointService> and return a new Endpoint.
§Limitations
Body-type-changing middleware (like CorsLayer and CompressionLayer from tower-http)
cannot be used with this method. These middleware change the HTTP response body type,
which is incompatible with the Endpoint trait that expects OxiditeResponse.
For such middleware, use ServiceBuilder instead:
let service = ServiceBuilder::new()
.layer(CorsLayer::permissive())
.layer(CompressionLayer::new())
.service(router);Sourcepub fn with_layer<L>(self, layer: L) -> Self
pub fn with_layer<L>(self, layer: L) -> Self
Alias for .layer().
Sourcepub fn with_cors(self, config: CorsConfig) -> Self
pub fn with_cors(self, config: CorsConfig) -> Self
Configure CORS for this router.
This adds CORS headers to all responses, including preflight OPTIONS requests. This is a framework-level CORS implementation that doesn’t require tower-http.
§Example
use oxidite::prelude::*;
let router = Router::new()
.with_cors(CorsConfig {
allowed_origins: vec!["http://localhost:3000".to_string()],
allowed_methods: vec!["GET".to_string(), "POST".to_string()],
allowed_headers: vec!["Content-Type".to_string(), "Authorization".to_string()],
allow_credentials: true,
max_age: 3600,
});For development, you can use the permissive config:
let router = Router::new()
.with_cors(CorsConfig::permissive());pub async fn handle(&self, req: OxiditeRequest) -> Result<OxiditeResponse>
Trait Implementations§
Source§impl Service<Request<BoxBody<Bytes, Error>>> for Router
impl Service<Request<BoxBody<Bytes, Error>>> for Router
Source§type Response = OxiditeResponse
type Response = OxiditeResponse
Source§type Future = Pin<Box<dyn Future<Output = Result<<Router as Service<Request<BoxBody<Bytes, Error>>>>::Response, Error>> + Send>>
type Future = Pin<Box<dyn Future<Output = Result<<Router as Service<Request<BoxBody<Bytes, Error>>>>::Response, Error>> + Send>>
Auto Trait Implementations§
impl !RefUnwindSafe for Router
impl !UnwindSafe for Router
impl Freeze for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Router
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
Service, calling it with the provided request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
poll_ready method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
poll_ready method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
poll_ready method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Result<Self::Response, Self::Error>)
to a different value, regardless of whether the future succeeds or
fails. Read moreSource§fn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F>
fn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F>
Source§fn filter_async<F, NewRequest>(self, filter: F) -> AsyncFilter<Self, F>where
Self: Sized,
F: AsyncPredicate<NewRequest>,
fn filter_async<F, NewRequest>(self, filter: F) -> AsyncFilter<Self, F>where
Self: Sized,
F: AsyncPredicate<NewRequest>,
AsyncFilter that conditionally accepts or
rejects requests based on an [async predicate]. Read more