Struct salvo_core::prelude::Service
source · #[non_exhaustive]pub struct Service {
pub router: Arc<Router>,
pub catcher: Option<Arc<Catcher>>,
pub hoops: Vec<Arc<dyn Handler>>,
pub allowed_media_types: Arc<Vec<Mime>>,
}
Expand description
Service http request.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.router: Arc<Router>
The router of this service.
catcher: Option<Arc<Catcher>>
The catcher of this service.
hoops: Vec<Arc<dyn Handler>>
These hoops will alwways be called when request received.
allowed_media_types: Arc<Vec<Mime>>
The allowed media types of this service.
Implementations§
source§impl Service
impl Service
sourcepub fn catcher(self, catcher: impl Into<Arc<Catcher>>) -> Self
pub fn catcher(self, catcher: impl Into<Arc<Catcher>>) -> Self
When the response code is 400-600 and the body is empty, capture and set the error page content. If catchers is not set, the default error page will be used.
§Example
#[handler]
async fn handle404(&self, _req: &Request, _depot: &Depot, res: &mut Response, ctrl: &mut FlowCtrl) {
if let Some(StatusCode::NOT_FOUND) = res.status_code {
res.render("Custom 404 Error Page");
ctrl.skip_rest();
}
}
#[tokio::main]
async fn main() {
Service::new(Router::new()).catcher(Catcher::default().hoop(handle404));
}
sourcepub fn hoop<H: Handler>(self, hoop: H) -> Self
pub fn hoop<H: Handler>(self, hoop: H) -> Self
Add a handler as middleware, it will run the handler when request received.
sourcepub fn hoop_when<H, F>(self, hoop: H, filter: F) -> Self
pub fn hoop_when<H, F>(self, hoop: H, filter: F) -> Self
Add a handler as middleware, it will run the handler when request received.
This middleware only effective when the filter return true.
sourcepub fn allowed_media_types<T>(self, allowed_media_types: T) -> Self
pub fn allowed_media_types<T>(self, allowed_media_types: T) -> Self
Sets allowed media types list and returns Self
for write code chained.
§Example
let service = Service::new(Router::new()).allowed_media_types(vec![mime::TEXT_PLAIN]);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Service
impl !RefUnwindSafe for Service
impl Send for Service
impl Sync for Service
impl Unpin for Service
impl !UnwindSafe for Service
Blanket Implementations§
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Mutably borrows from an owned value. Read more