Skip to main content

Router

Struct Router 

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

A modular router for grouping and mounting routes under a common base path.

The Router struct allows you to organize related routes together and mount them onto an application at a specified base path. This is useful for building APIs with versioning, grouping endpoints, or composing applications from multiple routers.

§Example

use ripress::{router::Router, app::App};
use ripress::{req::HttpRequest, res::HttpResponse};
use ripress::types::RouterFns;

async fn handler(req: HttpRequest, res: HttpResponse) -> HttpResponse {
    res.ok().text("Hello, World!")
}

let mut router = Router::new("/api");
router.get("/hello", handler);
let mut app = App::new();
app.router(router);

Implementations§

Source§

impl Router

Source

pub fn new(base_path: &'static str) -> Self

Creates a new Router instance with the specified base path.

The base path determines the prefix under which all routes registered to this router will be mounted when attached to an application.

§Arguments
  • base_path - The base path (e.g., “/api” or “/v1”) for this router.
§Returns

A new Router with an empty set of routes, ready for route registration.

§Example
use ripress::router::Router;
let router = Router::new("/api");
Source

pub fn register(self, app: &mut App)

👎Deprecated since 1.9.12:

use app.router instead

Registers a router with an app.

§Arguments
  • mut app - The instance of the app to register the router too
§Example
use ripress::{router::Router, app::App};
use ripress::{req::HttpRequest, res::HttpResponse};
use ripress::types::RouterFns;

async fn handler(req: HttpRequest, res: HttpResponse) -> HttpResponse {
    res.ok().text("Hello, World!")
}

let mut router = Router::new("/api");
let mut app = App::new();
router.patch("/hello", handler);
app.router(router);

Trait Implementations§

Source§

impl RouterFns for Router

Source§

fn routes( &mut self, ) -> &mut HashMap<String, HashMap<HttpMethods, Arc<dyn Fn(HttpRequest, HttpResponse) -> Pin<Box<dyn Future<Output = HttpResponse> + Send + 'static>> + Send + Sync + 'static>>>

Get a mutable reference to the internal routes map. Read more
Source§

fn add_route<F, HFut>(&mut self, method: HttpMethods, path: &str, handler: F)
where F: Fn(HttpRequest, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static,

Register a handler for a specific HTTP method/path. Read more
Source§

fn get<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Self
where F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static, P: ExtractFromOwned + Send + 'static,

Register a GET handler for a path, with extractor integration. Read more
Source§

fn options<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Self
where F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static, P: ExtractFromOwned + Send + 'static,

Register an OPTIONS handler for a path, with extractor integration.
Source§

fn post<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Self
where F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static, P: ExtractFromOwned + Send + 'static,

Register a POST handler for a path, with extractor integration.
Source§

fn put<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Self
where F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static, P: ExtractFromOwned + Send + 'static,

Register a PUT handler for a path, with extractor integration.
Source§

fn delete<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Self
where F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static, P: ExtractFromOwned + Send + 'static,

Register a DELETE handler for a path, with extractor integration.
Source§

fn head<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Self
where F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static, P: ExtractFromOwned + Send + 'static,

Register a HEAD handler for a path, with extractor integration.
Source§

fn patch<F, HFut, P>(&mut self, path: &str, handler: F) -> &mut Self
where F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static, P: ExtractFromOwned + Send + 'static,

Register a PATCH handler for a path, with extractor integration.
Source§

fn get_routes( &mut self, path: &str, method: HttpMethods, ) -> Option<&Arc<dyn Fn(HttpRequest, HttpResponse) -> Pin<Box<dyn Future<Output = HttpResponse> + Send + 'static>> + Send + Sync + 'static>>

Retrieve the route handler for a given path/method, if one is registered. Read more
Source§

fn add_route_with_extraction<F, HFut, P>( &mut self, method: HttpMethods, path: &str, handler: F, )
where F: Fn(P, HttpResponse) -> HFut + Send + Sync + 'static, HFut: Future<Output = HttpResponse> + Send + 'static, P: ExtractFromOwned + Send + 'static,

Internal helper: Register a handler using extractor integration. Read more

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