pub struct Router { /* private fields */ }Implementations§
Source§impl Router
impl Router
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/middleware.rs (line 69)
57fn main() {
58 let mut app = Server::new();
59
60 // Apply logger middleware globally
61 app.middleware(Logger);
62
63 // Public route - no auth required
64 app.get("/public", |_req| async {
65 Response::text("This is a public endpoint")
66 });
67
68 // Protected routes with auth middleware
69 let mut protected = Router::new();
70 protected.middleware(AuthMiddleware);
71
72 protected.get("/profile", |_req| async {
73 ok_json!({
74 "name": "User",
75 "email": "user@example.com"
76 })
77 });
78
79 app.mount("/api", protected);
80
81 app.listen("127.0.0.1:3000")
82 .expect("Server failed to start");
83}More examples
examples/routing.rs (line 41)
18fn main() {
19 let mut app = Server::new();
20
21 // Basic GET route
22 app.get("/", |_req| async {
23 Response::text("Welcome to Axeon API server!")
24 });
25
26 // Route with path parameter
27 app.get("/users/:id", |req| async move {
28 let user_id = req.params.get("id").unwrap();
29 Response::text(format!("User ID: {}", user_id))
30 });
31
32 // POST request with JSON body
33 app.post("/users", |req| async move {
34 match req.body.json::<User>() {
35 Some(user) => Response::ok(&user),
36 None => Err(ServerError::BadRequest("Invalid JSON body".to_string())),
37 }
38 });
39
40 // Group routes under /api prefix
41 let mut api = Router::new();
42 api.get("/status", |_req| async {
43 Response::ok(&serde_json::json!({
44 "status": "operational",
45 "version": "1.0.0"
46 }))
47 });
48
49 // Mount the API router to the main server
50 app.mount("/api", api);
51
52 app.listen("127.0.0.1:3000")
53 .expect("Server failed to start")
54}Sourcepub fn get<F, R>(&mut self, path: &str, handler: F) -> &mut Self
pub fn get<F, R>(&mut self, path: &str, handler: F) -> &mut Self
Examples found in repository?
examples/middleware.rs (lines 72-77)
57fn main() {
58 let mut app = Server::new();
59
60 // Apply logger middleware globally
61 app.middleware(Logger);
62
63 // Public route - no auth required
64 app.get("/public", |_req| async {
65 Response::text("This is a public endpoint")
66 });
67
68 // Protected routes with auth middleware
69 let mut protected = Router::new();
70 protected.middleware(AuthMiddleware);
71
72 protected.get("/profile", |_req| async {
73 ok_json!({
74 "name": "User",
75 "email": "user@example.com"
76 })
77 });
78
79 app.mount("/api", protected);
80
81 app.listen("127.0.0.1:3000")
82 .expect("Server failed to start");
83}More examples
examples/routing.rs (lines 42-47)
18fn main() {
19 let mut app = Server::new();
20
21 // Basic GET route
22 app.get("/", |_req| async {
23 Response::text("Welcome to Axeon API server!")
24 });
25
26 // Route with path parameter
27 app.get("/users/:id", |req| async move {
28 let user_id = req.params.get("id").unwrap();
29 Response::text(format!("User ID: {}", user_id))
30 });
31
32 // POST request with JSON body
33 app.post("/users", |req| async move {
34 match req.body.json::<User>() {
35 Some(user) => Response::ok(&user),
36 None => Err(ServerError::BadRequest("Invalid JSON body".to_string())),
37 }
38 });
39
40 // Group routes under /api prefix
41 let mut api = Router::new();
42 api.get("/status", |_req| async {
43 Response::ok(&serde_json::json!({
44 "status": "operational",
45 "version": "1.0.0"
46 }))
47 });
48
49 // Mount the API router to the main server
50 app.mount("/api", api);
51
52 app.listen("127.0.0.1:3000")
53 .expect("Server failed to start")
54}pub fn post<F, R>(&mut self, path: &str, handler: F) -> &mut Self
pub fn put<F, R>(&mut self, path: &str, handler: F) -> &mut Self
pub fn patch<F, R>(&mut self, path: &str, handler: F) -> &mut Self
pub fn delete<F, R>(&mut self, path: &str, handler: F) -> &mut Self
pub fn head<F, R>(&mut self, path: &str, handler: F) -> &mut Self
pub fn connect<F, R>(&mut self, path: &str, handler: F) -> &mut Self
pub fn options<F, R>(&mut self, path: &str, handler: F) -> &mut Self
pub fn trace<F, R>(&mut self, path: &str, handler: F) -> &mut Self
Sourcepub fn middleware(&mut self, middleware: impl Middleware + 'static)
pub fn middleware(&mut self, middleware: impl Middleware + 'static)
Examples found in repository?
examples/middleware.rs (line 70)
57fn main() {
58 let mut app = Server::new();
59
60 // Apply logger middleware globally
61 app.middleware(Logger);
62
63 // Public route - no auth required
64 app.get("/public", |_req| async {
65 Response::text("This is a public endpoint")
66 });
67
68 // Protected routes with auth middleware
69 let mut protected = Router::new();
70 protected.middleware(AuthMiddleware);
71
72 protected.get("/profile", |_req| async {
73 ok_json!({
74 "name": "User",
75 "email": "user@example.com"
76 })
77 });
78
79 app.mount("/api", protected);
80
81 app.listen("127.0.0.1:3000")
82 .expect("Server failed to start");
83}pub fn mount(&mut self, path: &str, router: Router)
Trait Implementations§
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> 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