Struct salvo::prelude::Router

source ·
#[non_exhaustive]
pub struct Router { pub routers: Vec<Router>, pub filters: Vec<Box<dyn Filter>>, pub hoops: Vec<Arc<dyn Handler>>, pub goal: Option<Arc<dyn Handler>>, /* private fields */ }
Expand description

Router struct is used for route request to different handlers.

You can write routers in flat way, like this:

§Example


Router::with_path("writers").get(list_writers).post(create_writer);
Router::with_path("writers/<id>").get(show_writer).patch(edit_writer).delete(delete_writer);
Router::with_path("writers/<id>/articles").get(list_writer_articles);

You can write router like a tree, this is also the recommended way:

§Example

use salvo_core::prelude::*;

Router::with_path("writers")
    .get(list_writers)
    .post(create_writer)
    .push(
        Router::with_path("<id>")
            .get(show_writer)
            .patch(edit_writer)
            .delete(delete_writer)
            .push(Router::with_path("articles").get(list_writer_articles)),
    );

This form of definition can make the definition of router clear and simple for complex projects.

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.
§routers: Vec<Router>

The children of current router.

§filters: Vec<Box<dyn Filter>>

The filters of current router.

§hoops: Vec<Arc<dyn Handler>>

The middlewares of current router.

§goal: Option<Arc<dyn Handler>>

The final handler to handle request of current router.

Implementations§

source§

impl Router

source

pub fn new() -> Router

Create a new Router.

source

pub fn routers(&self) -> &Vec<Router>

Get current router’s children reference.

source

pub fn routers_mut(&mut self) -> &mut Vec<Router>

Get current router’s children mutable reference.

source

pub fn hoops(&self) -> &Vec<Arc<dyn Handler>>

Get current router’s middlewares reference.

source

pub fn hoops_mut(&mut self) -> &mut Vec<Arc<dyn Handler>>

Get current router’s middlewares mutable reference.

source

pub fn filters(&self) -> &Vec<Box<dyn Filter>>

Get current router’s filters reference.

source

pub fn filters_mut(&mut self) -> &mut Vec<Box<dyn Filter>>

Get current router’s filters mutable reference.

source

pub fn detect( &self, req: &mut Request, path_state: &mut PathState ) -> Option<DetectMatched>

Detect current router is matched for current request.

source

pub fn unshift(self, router: Router) -> Router

Insert a router at the begining of current router, shifting all routers after it to the right.

source

pub fn insert(self, index: usize, router: Router) -> Router

Insert a router at position index within current router, shifting all routers after it to the right.

source

pub fn push(self, router: Router) -> Router

Push a router as child of current router.

source

pub fn append(self, others: &mut Vec<Router>) -> Router

Append all routers in a Vec as children of current router.

source

pub fn with_hoop<H>(hoop: H) -> Router
where H: Handler,

Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request.

source

pub fn with_hoop_when<H, F>(hoop: H, filter: F) -> Router
where H: Handler, F: Fn(&Request, &Depot) -> bool + Send + Sync + 'static,

Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request. This middleware only effective when the filter return true.

source

pub fn hoop<H>(self, hoop: H) -> Router
where H: Handler,

Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request.

source

pub fn hoop_when<H, F>(self, hoop: H, filter: F) -> Router
where H: Handler, F: Fn(&Request, &Depot) -> bool + Send + Sync + 'static,

Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request. This middleware only effective when the filter return true.

source

pub fn with_path(path: impl Into<String>) -> Router

Create a new router and set path filter.

§Panics

Panics if path value is not in correct format.

source

pub fn path(self, path: impl Into<String>) -> Router

Create a new path filter for current router.

§Panics

Panics if path value is not in correct format.

source

pub fn with_filter(filter: impl Filter) -> Router

Create a new router and set filter.

source

pub fn filter(self, filter: impl Filter) -> Router

Add a filter for current router.

source

pub fn with_filter_fn<T>(func: T) -> Router
where T: Fn(&mut Request, &mut PathState) -> bool + Send + Sync + 'static,

Create a new router and set filter_fn.

source

pub fn filter_fn<T>(self, func: T) -> Router
where T: Fn(&mut Request, &mut PathState) -> bool + Send + Sync + 'static,

Create a new FnFilter from Fn.

source

pub fn goal<H>(self, goal: H) -> Router
where H: Handler,

Sets current router’s handler.

source

pub fn then<F>(self, func: F) -> Router
where F: FnOnce(Router) -> Router,

When you want write router chain, this function will be useful, You can write your custom logic in FnOnce.

source

pub fn scheme(self, scheme: Scheme) -> Router

Add a SchemeFilter to current router.

source

pub fn host(self, host: impl Into<String>) -> Router

Add a HostFilter to current router.

source

pub fn port(self, port: u16) -> Router

Add a PortFilter to current router.

source

pub fn get<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter get method and set this child router’s handler.

source

pub fn post<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter post method and set this child router’s handler.

source

pub fn put<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter put method and set this child router’s handler.

source

pub fn delete<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter delete method and set this child router’s handler.

source

pub fn patch<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter patch method and set this child router’s handler.

source

pub fn head<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter head method and set this child router’s handler.

source

pub fn options<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter options method and set this child router’s handler.

Trait Implementations§

source§

impl Debug for Router

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Router

source§

fn default() -> Router

Returns the “default value” for a type. Read more
source§

impl RouterExt for Router

source§

fn oapi_security(self, security: SecurityRequirement) -> Router

Add security requirement to the router. Read more
source§

fn oapi_securities<I>(self, iter: I) -> Router

Add security requirements to the router. Read more
source§

fn oapi_tag(self, tag: impl Into<String>) -> Router

Add tag to the router. Read more
source§

fn oapi_tags<I, V>(self, iter: I) -> Router
where I: IntoIterator<Item = V>, V: Into<String>,

Add tags to the router. Read more
source§

impl SendTarget for Router

source§

async fn call(self, req: Request) -> Response

Send request to target, such as Router, Service, Handler.

Auto Trait Implementations§

§

impl Freeze for Router

§

impl !RefUnwindSafe for Router

§

impl Send for Router

§

impl Sync for Router

§

impl Unpin for Router

§

impl !UnwindSafe for Router

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

source§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more