Struct Routes

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

The Routes struct is used to manage API routes.

It allows for the addition of routes with or without password protection, and provides a method to apply these routes to an Actix Web ServiceConfig.

§Example

use rusty_api::{Routes, Method};
use actix_web::{HttpRequest, HttpResponse};
 
async fn public_route(_req: HttpRequest) -> HttpResponse {
    HttpResponse::Ok().body("Public route accessed!")
}
 
async fn protected_route(_req: HttpRequest) -> HttpResponse {
    HttpResponse::Ok().body("Protected route accessed!")
}
 
let routes = Routes::new()
    .add_route(Method::GET, "/public", public_route)
    .add_route_with_password(Method::GET, "/protected", protected_route, "SecretPassword");

Implementations§

Source§

impl Routes

Source

pub fn new() -> Self

Create a new Routes instance.

This initializes an empty collection of routes that can be configured and applied to an Actix Web ServiceConfig, via the Api struct.

§Example
use rusty_api::Routes;
use rusty_api::Api;

let routes = Routes::new();
let api = Api::new() 
    .configure_routes(routes);
Source

pub fn add_route_with_password<H, Args, R>( self, method: Method, path: &'static str, handler: H, password: &'static str, ) -> Self
where H: Handler<Args, Output = R> + Clone + Send + Sync + 'static, Args: FromRequest + 'static, R: Responder + 'static,

Add a new route to the Routes instance with password protection.

This method allows you to define a route that requires a password to access. The password is passed as a query parameter in the request.

§Arguments
  • method: The HTTP method for the route (e.g., GET, POST).
  • path: The URL path for the route.
  • handler: The handler function for the route.
  • password: The password required to access the route.
§Example
use rusty_api::{Routes, HttpRequest, HttpResponse, Method};

async fn protected_route(_req: HttpRequest) -> HttpResponse {
   HttpResponse::Ok().body("Protected route accessed!")
}

let routes = Routes::new()
   .add_route_with_password(Method::GET, "/protected", protected_route, "SecretPassword");
Source

pub fn add_route<H, Args, R>( self, method: Method, path: &'static str, handler: H, ) -> Self
where H: Handler<Args, Output = R> + Clone + Send + Sync + 'static, Args: FromRequest + 'static, R: Responder + 'static,

Add a new route to the Routes instance without password protection.

This method allows you to define a public route that does not require authentication.

§Arguments
  • method: The HTTP method for the route (e.g., GET, POST).
  • path: The URL path for the route.
  • handler: The handler function for the route.
§Example
use rusty_api::{Routes, HttpRequest, HttpResponse, Method};
 
async fn public_route(_req: HttpRequest) -> HttpResponse {
   HttpResponse::Ok().body("Public route accessed!")
}

let routes = Routes::new()
  .add_route(Method::GET, "/public", public_route);
Source

pub fn add_route_with_auth<H, R>( self, method: Method, path: &'static str, handler: H, ) -> Self
where H: Fn(HttpRequest, i32) -> R + Clone + Send + Sync + 'static, R: Future<Output = HttpResponse> + 'static,

Add a new route to the Routes instance with authentication.

This method allows you to define a route that requires authentication via a token. The token is passed in the Authorization header of the request.

§Arguments
  • method: The HTTP method for the route (e.g., GET, POST).
  • path: The URL path for the route.
  • handler: The handler function for the route.
§Example
use rusty_api::{Routes, HttpRequest, HttpResponse, Method};

async fn auth_route(_req: HttpRequest, userId: i32) -> HttpResponse {
   HttpResponse::Ok().body(format!("Authenticated user ID: {}", userId))
}

let routes = Routes::new()
   .add_route_with_auth(Method::GET, "/auth", auth_route);
Source

pub fn configure(&self, cfg: &mut ServiceConfig)

Apply the routes to a ServiceConfig.

This method iterates over all defined routes and applies them to the provided Axtix Web ServiceConfig. It is used internally by the Api struct.

§Arguments
  • cfg: A mutable reference to the ServiceConfig to which the routes will be applied.
§Example
use rusty_api::{Routes, Api};

let routes = Routes::new();

let api = Api::new()
   .configure_routes(routes); // The configure_routes method calls the configure method internally.

Auto Trait Implementations§

§

impl Freeze for Routes

§

impl !RefUnwindSafe for Routes

§

impl Send for Routes

§

impl Sync for Routes

§

impl Unpin for Routes

§

impl !UnwindSafe for Routes

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

Source§

type Output = T

Should always be Self
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
Source§

impl<T> ErasedDestructor for T
where T: 'static,