pub struct Routes { /* private fields */ }Expand description
A module’s routes, with optional names for URL generation.
Routes::new()
.get("/products", index).name("products.index")
.get("/products/{id}", show).name("products.show")
.post("/products", store).name("products.store")Implementations§
Source§impl Routes
impl Routes
Sourcepub fn get<H, T>(self, path: &str, handler: H) -> Self
pub fn get<H, T>(self, path: &str, handler: H) -> Self
Adds a route for this HTTP method (the function’s name) at path.
Sourcepub fn post<H, T>(self, path: &str, handler: H) -> Self
pub fn post<H, T>(self, path: &str, handler: H) -> Self
Adds a route for this HTTP method (the function’s name) at path.
Sourcepub fn put<H, T>(self, path: &str, handler: H) -> Self
pub fn put<H, T>(self, path: &str, handler: H) -> Self
Adds a route for this HTTP method (the function’s name) at path.
Sourcepub fn patch<H, T>(self, path: &str, handler: H) -> Self
pub fn patch<H, T>(self, path: &str, handler: H) -> Self
Adds a route for this HTTP method (the function’s name) at path.
Sourcepub fn delete<H, T>(self, path: &str, handler: H) -> Self
pub fn delete<H, T>(self, path: &str, handler: H) -> Self
Adds a route for this HTTP method (the function’s name) at path.
Sourcepub fn resource(self, path: &str, name: &str, resource: Resource) -> Self
pub fn resource(self, path: &str, name: &str, resource: Resource) -> Self
The routes of a resource, like Laravel’s Route::resource: only the
actions given, under path, named {name}.{action}.
| Action | Method and path | Name |
|---|---|---|
index | GET /products | products.index |
create | GET /products/new | products.create |
store | POST /products | products.store |
show | GET /products/{id} | products.show |
edit | GET /products/{id}/edit | products.edit |
update | PUT and PATCH /products/{id} | products.update |
destroy | DELETE /products/{id} | products.destroy |
Routes::new()
.resource("/products", "products", Resource::new().index(index).show(show).destroy(destroy))
.require_auth()Sourcepub fn route(self, path: &str, method_router: MethodRouter<AppState>) -> Self
pub fn route(self, path: &str, method_router: MethodRouter<AppState>) -> Self
Adds a route with any axum method router, e.g. get(show).post(update).
Sourcepub fn view(self, path: &str, template: &str) -> Self
pub fn view(self, path: &str, template: &str) -> Self
A page that needs no handler: GET path renders template with the
usual globals (Laravel’s Route::view), e.g. an “About” page.
Routes::new().view("/about", "pages/about.html").name("about")Sourcepub fn redirect(self, path: &str, to: &str) -> Self
pub fn redirect(self, path: &str, to: &str) -> Self
Sends path to to with a 302 (Laravel’s Route::redirect), e.g.
an old address.
Sourcepub fn permanent_redirect(self, path: &str, to: &str) -> Self
pub fn permanent_redirect(self, path: &str, to: &str) -> Self
Like redirect, with a 301: the move is permanent.
Sourcepub fn name(self, name: &str) -> Self
pub fn name(self, name: &str) -> Self
Names the route added just before, for use with url() and route() in templates.
§Panics
If no route has been added yet.
Sourcepub fn require_auth(self) -> Self
pub fn require_auth(self) -> Self
Only logged-in users may use the routes added so far; guests are sent
to the login route. Call it after adding the routes it should cover.
Sourcepub fn require_verified(self) -> Self
pub fn require_verified(self) -> Self
Like require_auth, and the user must have verified their email;
others are sent to the verification.notice route.
Sourcepub fn require_gate(self, name: &str) -> Self
pub fn require_gate(self, name: &str) -> Self
Only users the gate name lets through (App::gate, gate_async or
a permission of that name, after gate_before) may use the routes
added so far; guests are sent to log in, others get 403.
Sourcepub fn require_role(self, role: &str) -> Self
pub fn require_role(self, role: &str) -> Self
Only users with role (the Permissions module) may use the routes
added so far; guests are sent to log in, others get 403.
Sourcepub fn require_permission(self, permission: &str) -> Self
pub fn require_permission(self, permission: &str) -> Self
Only users granted permission (the Permissions module, after
gate_before) may use the routes added so far.
Sourcepub fn require_ability(self, ability: &str) -> Self
pub fn require_ability(self, ability: &str) -> Self
Requests with an API token must have ability
(User::create_token_with); sessions and unrestricted tokens pass.
Sourcepub fn require_password_confirmed(self) -> Self
pub fn require_password_confirmed(self) -> Self
Users who haven’t typed their password in the last three hours are
asked for it (/confirm-password, from the Auth module) before the
routes added so far, e.g. billing settings. Also marks auth.
Sourcepub fn guest_only(self) -> Self
pub fn guest_only(self) -> Self
Only guests may use the routes added so far; logged-in users are sent
to the home route (e.g. for login and registration pages).
Sourcepub fn throttle(self, max: u32, per: Duration) -> Self
pub fn throttle(self, max: u32, per: Duration) -> Self
Limits the routes added so far to max requests per per, counted per
logged-in user or per IP address. Over the limit: 429 with Retry-After.
Sourcepub fn throttle_by(self, name: &str) -> Self
pub fn throttle_by(self, name: &str) -> Self
Limits the routes added so far with the named limiter name
(App::rate_limiter), whose rule picks the limit per request.
Sourcepub fn webhook<W: Webhook>(self, path: &str) -> Self
pub fn webhook<W: Webhook>(self, path: &str) -> Self
Receives W’s webhooks at path (POST): verifies, stores once per
event and processes them in the queue. See renox::webhook. The
route is named webhooks.<provider>, skips CSRF and keeps working in
maintenance mode.
Sourcepub fn etag(self) -> Self
pub fn etag(self) -> Self
Gives the routes added so far an ETag (a hash of the page as sent),
and answers 304 Not Modified without the body when the browser
already has that version (If-None-Match). For pages fetched again
and again that rarely change, e.g. a catalogue or an API list.
Only GET/HEAD answers with status 200 and a body of at most
2 MB get one; streamed bodies don’t.
Sourcepub fn without_csrf(self) -> Self
pub fn without_csrf(self) -> Self
Lets the routes added so far be posted to without a CSRF token, for callers that have no session, such as a payment gateway’s webhook. Such a handler must check the request itself (e.g. its signature).
Sourcepub fn cors(self, origins: &[&str]) -> Self
pub fn cors(self, origins: &[&str]) -> Self
Lets browsers on origins (e.g. https://app.example.com, or *
for any) call the routes added so far with fetch: answers CORS
preflights and adds the Access-Control-Allow-* headers. Allows the
usual methods and the Content-Type, Authorization, Accept and
X-CSRF-Token headers. Use cors_layer for anything else.
Sourcepub fn cors_layer(self, layer: CorsLayer) -> Self
pub fn cors_layer(self, layer: CorsLayer) -> Self
Like cors, with a tower_http::cors::CorsLayer built by hand
(renox::cors::CorsLayer), e.g. to allow credentials.
Sourcepub fn route_layer<L>(self, layer: L) -> Selfwhere
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
pub fn route_layer<L>(self, layer: L) -> Selfwhere
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
Wraps the routes added so far in a tower layer (axum’s route_layer).
Sourcepub fn merge(self, other: impl Into<Routes>) -> Self
pub fn merge(self, other: impl Into<Routes>) -> Self
Adds the routes of other, with their names and layers.
Sourcepub fn fallback<H, T>(self, handler: H) -> Self
pub fn fallback<H, T>(self, handler: H) -> Self
What answers a request no route and no public file matches: a page
of your own instead of the 404 page (Laravel’s Route::fallback).
One per app, or per domain inside Routes::domain.
async fn missing(uri: axum::http::Uri) -> (StatusCode, String) {
(StatusCode::NOT_FOUND, format!("Nothing at {}. Try the search.", uri.path()))
}
Routes::new().fallback(missing)Sourcepub fn domain(self, pattern: &str, routes: impl Into<Routes>) -> Self
pub fn domain(self, pattern: &str, routes: impl Into<Routes>) -> Self
Routes served only on hosts matching pattern, e.g.
admin.example.com, or {account}.example.com with the account in
DomainParams (Laravel’s Route::domain).
Routes::new()
.get("/", home) // other hosts
.domain(
"admin.example.com",
Routes::new().get("/", dashboard).name("admin.dashboard").require_auth(),
)A host that matches a domain gets that domain’s routes only (all
modules’ routes for that pattern), plus Renox’s own (/health, the
scripts, public files); every other host gets the routes without a
domain. So the same path can mean different pages on different hosts.
route() gives the path, as for other routes. Matching uses the
Host header: behind a proxy, keep it (Caddy and nginx’s
proxy_set_header Host $host do).
Sourcepub fn group(self, path: &str, name: &str, routes: impl Into<Routes>) -> Self
pub fn group(self, path: &str, name: &str, routes: impl Into<Routes>) -> Self
Adds routes under a path prefix and a name prefix, like Laravel’s
Route::prefix('admin')->name('admin.')->group(…). Guards added to
routes cover only them; guards added after the group cover
everything added so far, as usual.
Routes::new().group(
"/admin",
"admin.",
Routes::new()
.get("/", dashboard).name("dashboard") // GET /admin, `admin.dashboard`
.get("/users", users).name("users") // GET /admin/users, `admin.users`
.require_auth(),
)§Panics
If path doesn’t start with / or ends with /.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Routes
impl !UnwindSafe for Routes
impl Freeze for Routes
impl Send for Routes
impl Sync for Routes
impl Unpin for Routes
impl UnsafeUnpin 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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