pub struct Route {
pub path: String,
pub method: Method,
pub tsr: bool,
/* private fields */
}Expand description
HTTP route with path pattern matching and middleware support.
Fields§
§path: StringOriginal path string used to create this route.
method: MethodHTTP method this route responds to.
tsr: boolWhether trailing slash redirection is enabled.
Implementations§
Source§impl Route
impl Route
Sourcepub fn new(
path: String,
method: Method,
handler: BoxHandler,
tsr: Option<bool>,
) -> Route
pub fn new( path: String, method: Method, handler: BoxHandler, tsr: Option<bool>, ) -> Route
Creates a new route with the specified path, method, and handler.
Sourcepub fn middleware<F, Fut, R>(&self, f: F) -> &Route
pub fn middleware<F, Fut, R>(&self, f: F) -> &Route
Adds middleware to this route’s execution chain.
Sourcepub fn plugin<P>(&self, plugin: P) -> &Route
Available on crate feature plugins only.
pub fn plugin<P>(&self, plugin: P) -> &Route
plugins only.Adds a plugin to this route.
Route-level plugins allow applying functionality like compression, CORS, or rate limiting to specific routes instead of globally. Plugins added to a route are initialized when the route is first accessed.
§Examples
use tako::{router::Router, Method, responder::Responder, types::Request};
use tako::plugins::cors::CorsBuilder;
let mut router = Router::new();
let route = router.route(Method::GET, "/api/data", handler);
// Apply CORS only to this route
let cors = CorsBuilder::new()
.allow_origin("https://example.com")
.build();
route.plugin(cors);Sourcepub fn version(&self, version: Version) -> &Route
pub fn version(&self, version: Version) -> &Route
Restricts this route to a specific HTTP protocol version.
Requests whose version() does not match are answered with
505 HTTP Version Not Supported. Set once at registration; later calls
are no-ops (lock-free reads in the hot path).
Sourcepub fn h09(&self) -> &Route
pub fn h09(&self) -> &Route
HTTP/0.9 guard. Shorthand for Route::version with http::Version::HTTP_09.
Sourcepub fn h10(&self) -> &Route
pub fn h10(&self) -> &Route
HTTP/1.0 guard. Shorthand for Route::version with http::Version::HTTP_10.
Sourcepub fn h11(&self) -> &Route
pub fn h11(&self) -> &Route
HTTP/1.1 guard. Shorthand for Route::version with http::Version::HTTP_11.
Sourcepub fn h2(&self) -> &Route
pub fn h2(&self) -> &Route
HTTP/2 guard. Shorthand for Route::version with http::Version::HTTP_2.
Sourcepub fn signals(&self) -> &SignalArbiter
Available on crate feature signals only.
pub fn signals(&self) -> &SignalArbiter
signals only.Returns a reference to this route’s signal arbiter.
Sourcepub fn signal_arbiter(&self) -> SignalArbiter
Available on crate feature signals only.
pub fn signal_arbiter(&self) -> SignalArbiter
signals only.Returns a clone of this route’s signal arbiter for shared usage.
Sourcepub fn on_signal<F, Fut>(&self, id: impl Into<String>, handler: F)
Available on crate feature signals only.
pub fn on_signal<F, Fut>(&self, id: impl Into<String>, handler: F)
signals only.Registers a handler for a named signal on this route’s arbiter.
Sourcepub async fn emit_signal(&self, signal: Signal)
Available on crate feature signals only.
pub async fn emit_signal(&self, signal: Signal)
signals only.Emits a signal through this route’s arbiter.
Sourcepub fn operation_id(&self, id: impl Into<String>) -> &Route
Available on crate features utoipa or vespera only.
pub fn operation_id(&self, id: impl Into<String>) -> &Route
utoipa or vespera only.Sourcepub fn summary(&self, summary: impl Into<String>) -> &Route
Available on crate features utoipa or vespera only.
pub fn summary(&self, summary: impl Into<String>) -> &Route
utoipa or vespera only.Sourcepub fn description(&self, description: impl Into<String>) -> &Route
Available on crate features utoipa or vespera only.
pub fn description(&self, description: impl Into<String>) -> &Route
utoipa or vespera only.Sourcepub fn tag(&self, tag: impl Into<String>) -> &Route
Available on crate features utoipa or vespera only.
pub fn tag(&self, tag: impl Into<String>) -> &Route
utoipa or vespera only.Sourcepub fn deprecated(&self) -> &Route
Available on crate features utoipa or vespera only.
pub fn deprecated(&self) -> &Route
utoipa or vespera only.Sourcepub fn response(&self, status: u16, description: impl Into<String>) -> &Route
Available on crate features utoipa or vespera only.
pub fn response(&self, status: u16, description: impl Into<String>) -> &Route
utoipa or vespera only.Sourcepub fn parameter(&self, param: OpenApiParameter) -> &Route
Available on crate features utoipa or vespera only.
pub fn parameter(&self, param: OpenApiParameter) -> &Route
utoipa or vespera only.Adds a parameter definition for this route in OpenAPI documentation.
§Examples
use tako::openapi::{OpenApiParameter, ParameterLocation};
router.route(Method::GET, "/users", list_users)
.parameter(OpenApiParameter {
name: "limit".to_string(),
location: ParameterLocation::Query,
description: Some("Maximum number of results".to_string()),
required: false,
});Sourcepub fn request_body(&self, body: OpenApiRequestBody) -> &Route
Available on crate features utoipa or vespera only.
pub fn request_body(&self, body: OpenApiRequestBody) -> &Route
utoipa or vespera only.Sets the request body description for this route in OpenAPI documentation.
§Examples
use tako::openapi::OpenApiRequestBody;
router.route(Method::POST, "/users", create_user)
.request_body(OpenApiRequestBody {
description: Some("User data to create".to_string()),
required: true,
content_type: "application/json".to_string(),
});Sourcepub fn security(&self, requirement: impl Into<String>) -> &Route
Available on crate features utoipa or vespera only.
pub fn security(&self, requirement: impl Into<String>) -> &Route
utoipa or vespera only.Sourcepub fn openapi_metadata(&self) -> Option<RouteOpenApi>
Available on crate features utoipa or vespera only.
pub fn openapi_metadata(&self) -> Option<RouteOpenApi>
utoipa or vespera only.Returns a clone of the OpenAPI metadata for this route, if any.
Sourcepub fn timeout(&self, duration: Duration) -> &Route
pub fn timeout(&self, duration: Duration) -> &Route
Sets a timeout for this route, overriding the router-level timeout.
When a request exceeds the timeout duration, the timeout fallback handler is invoked (if configured on the router) or a 408 Request Timeout response is returned.
§Examples
use std::time::Duration;
router.route(Method::POST, "/upload", upload_handler)
.timeout(Duration::from_secs(60));Sourcepub fn simd_json(&self, mode: SimdJsonMode) -> &Route
pub fn simd_json(&self, mode: SimdJsonMode) -> &Route
Configures the SIMD JSON dispatch behavior for this route.
When the simd feature is enabled, Json<T> can use the sonic_rs SIMD
parser for faster deserialization. By default, SIMD is used for payloads
above 2 MB. This method lets you override that threshold — or force
SIMD on/off — for individual routes.
Without the simd feature this setting is accepted but has no effect.
§Examples
use tako::extractors::json::SimdJsonMode;
// Always use SIMD for a heavy ingest endpoint
router.route(Method::POST, "/api/ingest", ingest)
.simd_json(SimdJsonMode::Always);
// Use SIMD only above 4 KB
router.route(Method::POST, "/api/batch", batch)
.simd_json(SimdJsonMode::Threshold(4096));
// Disable SIMD for a latency-sensitive tiny-payload route
router.route(Method::POST, "/api/ping", ping)
.simd_json(SimdJsonMode::Never);Auto Trait Implementations§
impl !Freeze for Route
impl !RefUnwindSafe for Route
impl Send for Route
impl Sync for Route
impl Unpin for Route
impl UnsafeUnpin for Route
impl !UnwindSafe for Route
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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