Struct ic_pluto::router::Router

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

source

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();
source

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());
source

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(),
    })
});
source

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(),
    })
});
source

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(),
    })
});
source

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(),
    })
});
source

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(),
    })
});
source

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(),
    })
});
source

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(),
    })
});
source

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(),
    })
});
source

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);
source

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(),
    })
});
source

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"]);

Trait Implementations§

source§

impl Clone for Router

source§

fn clone(&self) -> Router

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more

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<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> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

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.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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

§

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.