pub struct Router { /* private fields */ }Expand description
A router for HTTP requests. The router is used to register handlers for different HTTP methods and paths.
Implementations§
source§impl Router
impl Router
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new router. The router is used to register handlers for different HTTP methods and paths. The router can be used as a handler for a server.
§Examples
use pluto::router::Router;
let mut router = Router::new();sourcepub fn set_global_prefix(&mut self, p: String) -> &mut Self
pub fn set_global_prefix(&mut self, p: String) -> &mut Self
Set a prefix for all paths registered on the router.
§Examples
use pluto::router::Router;
let mut router = Router::new();
router.set_global_prefix("/api".to_string());sourcepub fn handle(
&mut self,
path: &str,
upgrade: bool,
method: Method,
handler: impl Handler + 'static
) -> &mut Self
pub fn handle( &mut self, path: &str, upgrade: bool, method: Method, handler: impl Handler + 'static ) -> &mut Self
Register a handler for a path and method. The handler is called for requests with a matching path and method.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use pluto::method::Method;
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.handle("/hello", false, Method::GET, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from GET",
})
.into(),
})
});sourcepub fn get(
&mut self,
path: &str,
upgrade: bool,
handler: impl Handler + 'static
) -> &mut Self
pub fn get( &mut self, path: &str, upgrade: bool, handler: impl Handler + 'static ) -> &mut Self
Register a handler for GET requests at a path. The handler is called for requests with the GET method and a matching path.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use pluto::method::Method;
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.get("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from GET",
})
.into(),
})
});sourcepub fn head(
&mut self,
path: &str,
upgrade: bool,
handler: impl Handler + 'static
) -> &mut Self
pub fn head( &mut self, path: &str, upgrade: bool, handler: impl Handler + 'static ) -> &mut Self
Register a handler for HEAD requests at a path. The handler is called for requests with the HEAD method and a matching path.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use pluto::method::Method;
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.head("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from HEAD",
})
.into(),
})
});sourcepub fn options(
&mut self,
path: &str,
upgrade: bool,
handler: impl Handler + 'static
) -> &mut Self
pub fn options( &mut self, path: &str, upgrade: bool, handler: impl Handler + 'static ) -> &mut Self
Register a handler for OPTIONS requests at a path. The handler is called for requests with the OPTIONS method and a matching path.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use pluto::method::Method;
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.options("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from OPTIONS",
})
.into(),
})
});sourcepub fn post(
&mut self,
path: &str,
upgrade: bool,
handler: impl Handler + 'static
) -> &mut Self
pub fn post( &mut self, path: &str, upgrade: bool, handler: impl Handler + 'static ) -> &mut Self
Register a handler for POST requests at a path. The handler is called for requests with the POST method and a matching path.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use pluto::method::Method;
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.post("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from POST",
})
.into(),
})
});sourcepub fn put(
&mut self,
path: &str,
upgrade: bool,
handler: impl Handler + 'static
) -> &mut Self
pub fn put( &mut self, path: &str, upgrade: bool, handler: impl Handler + 'static ) -> &mut Self
Register a handler for PUT requests at a path. The handler is called for requests with the PUT method and a matching path.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use pluto::method::Method;
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.put("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from PUT",
})
.into(),
})
});sourcepub fn patch(
&mut self,
path: &str,
upgrade: bool,
handler: impl Handler + 'static
) -> &mut Self
pub fn patch( &mut self, path: &str, upgrade: bool, handler: impl Handler + 'static ) -> &mut Self
Register a handler for PATCH requests at a path. The handler is called for requests with the PATCH method and a matching path.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use pluto::method::Method;
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.patch("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from PATCH",
})
.into(),
})
});sourcepub fn delete(
&mut self,
path: &str,
upgrade: bool,
handler: impl Handler + 'static
) -> &mut Self
pub fn delete( &mut self, path: &str, upgrade: bool, handler: impl Handler + 'static ) -> &mut Self
Register a handler for DELETE requests at a path. The handler is called for requests with the DELETE method and a matching path.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use pluto::method::Method;
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.delete("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from DELETE",
})
.into(),
})
});sourcepub fn handle_options(&mut self, handle: bool)
pub fn handle_options(&mut self, handle: bool)
Allow the router to handle OPTIONS requests. If enabled, the router will automatically respond to OPTIONS requests with the allowed methods for a path. If disabled, the router will respond to OPTIONS requests with a 404.
§Examples
use pluto::router::Router;
let mut router = Router::new();
router.handle_options(true);sourcepub fn global_options(
self,
upgrade: bool,
handler: impl Handler + 'static
) -> Self
pub fn global_options( self, upgrade: bool, handler: impl Handler + 'static ) -> Self
Register a default handler for not registered requests. The handler is called for requests when router can’t matching path or method to any handler.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.global_options(false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 404,
headers: HashMap::new(),
body: json!({
"statusCode": 404,
"message": "Not Found",
})
.into(),
})
});sourcepub fn allowed(&self, path: &str) -> Vec<&str>
pub fn allowed(&self, path: &str) -> Vec<&str>
Get the allowed methods for a path.
§Examples
use pluto::router::Router;
use pluto::http::{HttpRequest, HttpResponse};
use serde_json::json;
use std::collections::HashMap;
let mut router = Router::new();
router.get("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from GET",
})
.into(),
})
});
router.post("/hello", false, |req: HttpRequest| async move {
Ok(HttpResponse {
status_code: 200,
headers: HashMap::new(),
body: json!({
"statusCode": 200,
"message": "Hello World from POST",
})
.into(),
})
});
let mut allowed = router.allowed("/hello");
allowed.sort();
assert_eq!(allowed, vec!["GET", "OPTIONS", "POST"]);