Struct mco_http::route::Route

source ·
pub struct Route {
    pub container: SyncHashMap<String, Arc<Box<dyn Any>>>,
    pub middleware: SyncVec<Box<dyn MiddleWare>>,
    pub handlers: SyncHashMap<String, HandleBox>,
}

Fields§

§container: SyncHashMap<String, Arc<Box<dyn Any>>>§middleware: SyncVec<Box<dyn MiddleWare>>§handlers: SyncHashMap<String, HandleBox>

Implementations§

source§

impl Route

source

pub fn new() -> Self

source

pub fn handle_fn<H: Handler + 'static>(&self, url: &str, h: H)

handle a fn for example:

use mco_http::route::Route;
use mco_http::server::{Request, Response};

let mut route = Route::new();
//Common way
route.handle_fn("/", |req: Request, res: Response| {
        res.send(b"Hello World!").unwrap();
    });

//or you can use method. It can even nest calls to Handle
fn hello(req: Request, res: Response) {
    res.send(b"Hello World!").unwrap();
}
route.handle_fn("/",hello);

source

pub fn add_middleware<M: MiddleWare + 'static>(&self, m: M)

if you take Response. handle be done for example:

use mco_http::server::{Request, Response};
use mco_http::route::Route;
let mut route = Route::new();
route.add_middleware(|req: &mut Request, res: &mut Option<Response>|{
       //res.take() //take Response, next handle will be not run
    });
source

pub fn insert<T: Any>(&self, key: &str, data: T)

source

pub fn get<T: Any>(&self, key: &str) -> Option<&T>

source

pub fn index<T: Any>(&self, key: &str) -> &T

index will get from container.if not exist will be panic!

Trait Implementations§

source§

impl Debug for Route

source§

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

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

impl Handler for Route

source§

fn handle<'a, 'k>(&'a self, req: Request<'a, 'k>, res: Response<'a, Fresh>)

Receives a Request/Response pair, and should perform some action on them. Read more
source§

fn check_continue(&self, _: (&Method, &RequestUri, &Headers)) -> StatusCode

Called when a Request includes a Expect: 100-continue header. Read more
source§

fn on_connection_start(&self)

This is run after a connection is received, on a per-connection basis (not a per-request basis, as a connection with keep-alive may handle multiple requests)
source§

fn on_connection_end(&self)

This is run before a connection is closed, on a per-connection basis (not a per-request basis, as a connection with keep-alive may handle multiple requests)

Auto Trait Implementations§

§

impl !RefUnwindSafe for Route

§

impl Send for Route

§

impl Sync for Route

§

impl Unpin for Route

§

impl !UnwindSafe for Route

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<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, 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
§

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

§

fn vzip(self) -> V