Struct salvo_core::Service
source · [−]pub struct Service { /* private fields */ }
Expand description
Service http request.
Implementations
sourceimpl Service
impl Service
sourcepub fn new<T>(router: T) -> Service where
T: Into<Arc<Router>>,
pub fn new<T>(router: T) -> Service where
T: Into<Arc<Router>>,
Create a new Service with a Router
.
sourcepub fn with_catchers<T>(self, catchers: T) -> Self where
T: Into<Arc<Vec<Box<dyn Catcher>>>>,
pub fn with_catchers<T>(self, catchers: T) -> Self where
T: Into<Arc<Vec<Box<dyn Catcher>>>>,
When the response code is 400-600 and the body is empty, capture and set the return value.
If catchers is not set, the default CatcherImpl
will be used.
Example
struct Handle404;
impl Catcher for Handle404 {
fn catch(&self, _req: &Request, _depot: &Depot, res: &mut Response) -> bool {
if let Some(StatusCode::NOT_FOUND) = res.status_code() {
res.render("Custom 404 Error Page");
true
} else {
false
}
}
}
#[tokio::main]
async fn main() {
let catchers: Vec<Box<dyn Catcher>> = vec![Box::new(Handle404)];
Service::new(Router::new()).with_catchers(catchers);
}
sourcepub fn with_allowed_media_types<T>(self, allowed_media_types: T) -> Self where
T: Into<Arc<Vec<Mime>>>,
pub fn with_allowed_media_types<T>(self, allowed_media_types: T) -> Self where
T: Into<Arc<Vec<Mime>>>,
Set allowed media types list and returns Self
for write code chained.
Example
let service = Service::new(Router::new()).with_allowed_media_types(vec![mime::TEXT_PLAIN]);
sourcepub fn allowed_media_types(&self) -> Arc<Vec<Mime>>
pub fn allowed_media_types(&self) -> Arc<Vec<Mime>>
Get allowed media types list.
sourcepub async fn handle(&self, request: Request) -> Response
pub async fn handle(&self, request: Request) -> Response
Handle Request
and returns Response
.
This function is useful for testing application.
Example
use salvo_core::prelude::*;
#[fn_handler]
async fn hello_world() -> &'static str {
"Hello World"
}
#[tokio::main]
async fn main() {
let service: Service = Router::new().get(hello_world).into();
let req = hyper::Request::builder().method("GET").uri("http://127.0.0.1:7878");
let req: Request = req.body(hyper::Body::empty()).unwrap().into();
assert_eq!(service.handle(req).await.take_text().await.unwrap(), "Hello World");
}
Trait Implementations
sourceimpl<'t, T> Service<&'t T> for Service where
T: Transport,
impl<'t, T> Service<&'t T> for Service where
T: Transport,
type Response = HyperHandler
type Response = HyperHandler
Responses given by the service.
type Future = Ready<Result<<Service as Service<&'t T>>::Response, <Service as Service<&'t T>>::Error>>
type Future = Ready<Result<<Service as Service<&'t T>>::Response, <Service as Service<&'t T>>::Error>>
The future response value.
Auto Trait Implementations
impl !RefUnwindSafe for Service
impl Send for Service
impl Sync for Service
impl Unpin for Service
impl !UnwindSafe for Service
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more