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
impl Routes
Sourcepub fn new() -> Self
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);Sourcepub fn add_route_with_password<H, Args, R>(
self,
method: Method,
path: &'static str,
handler: H,
password: &'static str,
) -> Self
pub fn add_route_with_password<H, Args, R>( self, method: Method, path: &'static str, handler: H, password: &'static str, ) -> Self
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");Sourcepub fn add_route<H, Args, R>(
self,
method: Method,
path: &'static str,
handler: H,
) -> Self
pub fn add_route<H, Args, R>( self, method: Method, path: &'static str, handler: H, ) -> Self
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);Sourcepub fn add_route_with_auth<H, R>(
self,
method: Method,
path: &'static str,
handler: H,
) -> Selfwhere
H: Fn(HttpRequest, i32) -> R + Clone + Send + Sync + 'static,
R: Future<Output = HttpResponse> + 'static,
pub fn add_route_with_auth<H, R>(
self,
method: Method,
path: &'static str,
handler: H,
) -> Selfwhere
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);Sourcepub fn configure(&self, cfg: &mut ServiceConfig)
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 theServiceConfigto 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> 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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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