Skip to main content

Router

Struct Router 

Source
pub struct Router { /* private fields */ }

Implementations§

Source§

impl Router

Source

pub fn get<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Examples found in repository?
examples/02_controller.rs (line 11)
8pub fn main() {
9    let server = server("127.0.0.1", 9999);
10
11    server.router().get("/", index);
12
13    println!("\r\n\r\nRunning Server: {}\r\n\r\n", server.address());
14
15    server.listen();
16}
More examples
Hide additional examples
examples/01_hello_world.rs (lines 6-8)
3pub fn main() {
4    let server = server("127.0.0.1", 9999);
5
6    server.router().get("/", async |_req, res| {
7        res.html("<h1>Hello World</h1>")
8    });
9
10    println!("\r\n\r\nRunning Server: {}\r\n\r\n", server.address());
11
12    server.listen();
13}
examples/09_session.rs (line 33)
29fn main() {
30    let server = server("127.0.0.1", 9999);
31
32    server.router().group("/", |router| {
33        router.get("/", home_view);
34        router.get("login", login);
35        router.get("logout", logout);
36    });
37
38    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
39
40    server.listen();
41}
examples/11_multipart.rs (line 36)
31fn main() {
32    let server = server("127.0.0.1", 9999)
33        .storage("default", LocalStorage::new("storage"));
34
35    server.router().group("/", |router| {
36        router.get("/", home);
37        router.post("upload", upload);
38    });
39
40    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
41
42    server.listen();
43}
examples/15_hook.rs (lines 34-37)
31pub fn main() {
32    let server = server("127.0.0.1", 9999);
33
34    server.router().get("/", async |_req, res| {
35        println!("CONTROLLER");
36        res.html("<h1>Hello controller</h1>")
37    });
38
39    server.hook(CustomHook::new());
40
41    println!("\r\n\r\nRunning Server: {}\r\n\r\n", server.address());
42
43    server.listen();
44}
examples/10_cookie.rs (line 33)
29fn main() {
30    let server = server("127.0.0.1", 9999);
31
32    server.router().group("/", |router| {
33        router.get("/", home_view);
34        router.get("cookie", cookie);
35        router.delete("cookie/remove", remove_cookie);
36    });
37
38    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
39
40    server.listen();
41}
Source

pub fn post<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Examples found in repository?
examples/11_multipart.rs (line 37)
31fn main() {
32    let server = server("127.0.0.1", 9999)
33        .storage("default", LocalStorage::new("storage"));
34
35    server.router().group("/", |router| {
36        router.get("/", home);
37        router.post("upload", upload);
38    });
39
40    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
41
42    server.listen();
43}
More examples
Hide additional examples
examples/12_validation.rs (line 43)
39fn main() {
40    let server = server("127.0.0.1", 9999);
41
42    server.router().group("/", |router| {
43        router.post("register", register).middleware(register_form);
44        router.post("documents", upload).middleware(upload_documents_form);
45    });
46
47    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
48
49    server.listen();
50}
examples/04_advanced_routing.rs (line 52)
46fn main() {
47    let server = server("127.0.0.1", 9999);
48    
49    server.router().group("/", |router| {
50        router.group("users", |router| {
51            router.get("/", index);
52            router.post("/", store);
53            router.group("{user}", |router| {
54                router.get("/", view);
55                router.patch("/", update);
56                router.delete("/", destroy);
57            });
58        });
59    });
60
61    server.router().not_found(not_found);
62
63    server.error(error);
64
65    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
66
67    server.listen();
68}
Source

pub fn put<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Source

pub fn patch<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Examples found in repository?
examples/04_advanced_routing.rs (line 55)
46fn main() {
47    let server = server("127.0.0.1", 9999);
48    
49    server.router().group("/", |router| {
50        router.group("users", |router| {
51            router.get("/", index);
52            router.post("/", store);
53            router.group("{user}", |router| {
54                router.get("/", view);
55                router.patch("/", update);
56                router.delete("/", destroy);
57            });
58        });
59    });
60
61    server.router().not_found(not_found);
62
63    server.error(error);
64
65    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
66
67    server.listen();
68}
Source

pub fn delete<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Examples found in repository?
examples/10_cookie.rs (line 35)
29fn main() {
30    let server = server("127.0.0.1", 9999);
31
32    server.router().group("/", |router| {
33        router.get("/", home_view);
34        router.get("cookie", cookie);
35        router.delete("cookie/remove", remove_cookie);
36    });
37
38    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
39
40    server.listen();
41}
More examples
Hide additional examples
examples/04_advanced_routing.rs (line 56)
46fn main() {
47    let server = server("127.0.0.1", 9999);
48    
49    server.router().group("/", |router| {
50        router.group("users", |router| {
51            router.get("/", index);
52            router.post("/", store);
53            router.group("{user}", |router| {
54                router.get("/", view);
55                router.patch("/", update);
56                router.delete("/", destroy);
57            });
58        });
59    });
60
61    server.router().not_found(not_found);
62
63    server.error(error);
64
65    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
66
67    server.listen();
68}
Source

pub fn copy<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Source

pub fn head<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Source

pub fn options<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Source

pub fn route<C, Fut>( &mut self, method: impl Into<String>, path: impl Into<String>, callback: C, ) -> &mut Route<HttpHandler>
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Source

pub fn not_found<C, Fut>(&mut self, callback: C)
where C: Fn(Request, Response) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Examples found in repository?
examples/03_routing.rs (line 26)
17fn main() {
18    let server = server("127.0.0.1", 9999);
19
20    server.router().group("/", |router| {
21        router.get("/", async |_req, res| {
22            return res.html("<h1>Hello World!!!</h1>")
23        });
24    });
25
26    server.router().not_found(not_found);
27
28    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
29
30    server.listen();
31}
More examples
Hide additional examples
examples/04_advanced_routing.rs (line 61)
46fn main() {
47    let server = server("127.0.0.1", 9999);
48    
49    server.router().group("/", |router| {
50        router.group("users", |router| {
51            router.get("/", index);
52            router.post("/", store);
53            router.group("{user}", |router| {
54                router.get("/", view);
55                router.patch("/", update);
56                router.delete("/", destroy);
57            });
58        });
59    });
60
61    server.router().not_found(not_found);
62
63    server.error(error);
64
65    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
66
67    server.listen();
68}
Source

pub fn ws<C, Fut>( &mut self, path: impl Into<String>, callback: C, ) -> &mut Route<WebsocketHandler>
where C: Fn(Request, Websocket) -> Fut + Send + Sync + 'static, Fut: Future<Output = Websocket> + Send + 'static,

Examples found in repository?
examples/13_websocket.rs (lines 7-21)
3fn main() {
4    let server = server("127.0.0.1", 9999);
5
6    server.router().group("", |router| {
7        router.ws("/", async |_req, ws| -> Websocket {
8            ws.on(async |event, writer| {
9                match event {
10                    flyer::websocket::Event::Ready() => todo!(),
11                    flyer::websocket::Event::Text(bytes) => {
12                        println!("Received: {}", String::from_utf8_lossy(&bytes));
13                        writer.write("Hello from WebSocket!".into()).unwrap();
14                    },
15                    flyer::websocket::Event::Binary(_bytes) => todo!(),
16                    flyer::websocket::Event::Ping(_bytes) => todo!(),
17                    flyer::websocket::Event::Pong(_bytes) => todo!(),
18                    flyer::websocket::Event::Close(_reason) => todo!(),
19                }
20            })
21        });
22    });
23
24    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
25
26    server.listen();
27}
Source

pub fn group(&mut self, path: &str, group: Group) -> &mut GroupRouter

Examples found in repository?
examples/09_session.rs (lines 32-36)
29fn main() {
30    let server = server("127.0.0.1", 9999);
31
32    server.router().group("/", |router| {
33        router.get("/", home_view);
34        router.get("login", login);
35        router.get("logout", logout);
36    });
37
38    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
39
40    server.listen();
41}
More examples
Hide additional examples
examples/11_multipart.rs (lines 35-38)
31fn main() {
32    let server = server("127.0.0.1", 9999)
33        .storage("default", LocalStorage::new("storage"));
34
35    server.router().group("/", |router| {
36        router.get("/", home);
37        router.post("upload", upload);
38    });
39
40    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
41
42    server.listen();
43}
examples/10_cookie.rs (lines 32-36)
29fn main() {
30    let server = server("127.0.0.1", 9999);
31
32    server.router().group("/", |router| {
33        router.get("/", home_view);
34        router.get("cookie", cookie);
35        router.delete("cookie/remove", remove_cookie);
36    });
37
38    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
39
40    server.listen();
41}
examples/08_middleware.rs (lines 28-32)
25fn main() {
26    let server = server("127.0.0.1", 9999);
27
28    server.router().group("api", |router| {
29        router.get("/", async |_req, res| {
30            return res.html("<h1>Authorized Access</h1>");
31        });
32    }).middleware(auth);
33
34    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
35
36    server.listen();
37}
examples/12_validation.rs (lines 42-45)
39fn main() {
40    let server = server("127.0.0.1", 9999);
41
42    server.router().group("/", |router| {
43        router.post("register", register).middleware(register_form);
44        router.post("documents", upload).middleware(upload_documents_form);
45    });
46
47    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
48
49    server.listen();
50}
examples/03_routing.rs (lines 20-24)
17fn main() {
18    let server = server("127.0.0.1", 9999);
19
20    server.router().group("/", |router| {
21        router.get("/", async |_req, res| {
22            return res.html("<h1>Hello World!!!</h1>")
23        });
24    });
25
26    server.router().not_found(not_found);
27
28    print!("\r\n\r\nRunning server: {}\r\n\r\n", server.address());
29
30    server.listen();
31}
Source

pub fn subdomain( &mut self, subdomain: impl Into<String>, group: Group, ) -> &mut GroupRouter

Source

pub fn middleware<C, Fut>(&mut self, callback: C) -> &mut Self
where C: for<'a> Fn(Request, Response, Next) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Trait Implementations§

Source§

impl Clone for Router

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl From<&Router> for Router

Source§

fn from(value: &Router) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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

Source§

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