pub struct Router { /* private fields */ }Expand description
Maps HTTP method + path pairs to handler functions.
Routes are inserted into a trie during registration, then frozen
via freeze() before serving. Static segments take priority over
parameterized segments (:name) during matching.
Implementations§
Source§impl Router
impl Router
Sourcepub fn max_request_body(self, bytes: usize) -> Self
pub fn max_request_body(self, bytes: usize) -> Self
Set the maximum request body size in bytes (capped at 256 MB).
Sourcepub fn sse_buffer_size(self, size: usize) -> Self
pub fn sse_buffer_size(self, size: usize) -> Self
Set the channel buffer size for SSE connections.
Controls how many events can be queued before backpressure applies. Default: 32.
Sourcepub fn ws_buffer_size(self, size: usize) -> Self
pub fn ws_buffer_size(self, size: usize) -> Self
Set the channel buffer size for WebSocket connections.
Controls how many messages can be queued in each direction before backpressure applies. Default: 32.
Sourcepub fn skip_middleware_for_internal(self, skip: bool) -> Self
pub fn skip_middleware_for_internal(self, skip: bool) -> Self
Skip middleware for internal routes (/health, /metrics, /debug/pprof/cpu).
Default: false (middleware applies to all routes including internal ones).
Set to true to restore the pre-v3 behavior where internal routes bypass middleware,
useful when health probes (Kubernetes, load balancers) cannot send auth headers.
Sourcepub fn use_middleware<F, Fut>(&mut self, mw: F)
pub fn use_middleware<F, Fut>(&mut self, mw: F)
Register async middleware that wraps all route handlers.
Middleware registered first executes outermost (wraps all later
middleware). Each middleware receives the request and a Next
handle — call next.call(req).await to continue the chain.
router.use_middleware(|req, next| async move {
let resp = next.call(req).await;
resp.with_header("X-Custom", "value")
});Sourcepub fn get<F, Fut, R>(&mut self, path: &str, handler: F)
pub fn get<F, Fut, R>(&mut self, path: &str, handler: F)
Register a GET handler for path.
Path segments beginning with : are captured as named parameters.
Sourcepub fn delete<F, Fut, R>(&mut self, path: &str, handler: F)
pub fn delete<F, Fut, R>(&mut self, path: &str, handler: F)
Register a DELETE handler for path.
Sourcepub fn head<F, Fut, R>(&mut self, path: &str, handler: F)
pub fn head<F, Fut, R>(&mut self, path: &str, handler: F)
Register a HEAD handler for path.
If you do not register one, Camber can still answer HEAD requests for matching GET routes by stripping the response body automatically.
Sourcepub fn options<F, Fut, R>(&mut self, path: &str, handler: F)
pub fn options<F, Fut, R>(&mut self, path: &str, handler: F)
Register an OPTIONS handler for path.
Sourcepub fn get_stream(
&mut self,
path: &str,
handler: impl Fn(&Request) -> Pin<Box<dyn Future<Output = StreamResponse> + Send>> + Send + Sync + 'static,
)
pub fn get_stream( &mut self, path: &str, handler: impl Fn(&Request) -> Pin<Box<dyn Future<Output = StreamResponse> + Send>> + Send + Sync + 'static, )
Register an async streaming handler for GET requests.
The handler returns a StreamResponse for incremental body delivery.
Use StreamResponse::new() to get both the response and a sender.
Sourcepub fn post_stream(
&mut self,
path: &str,
handler: impl Fn(&Request) -> Pin<Box<dyn Future<Output = StreamResponse> + Send>> + Send + Sync + 'static,
)
pub fn post_stream( &mut self, path: &str, handler: impl Fn(&Request) -> Pin<Box<dyn Future<Output = StreamResponse> + Send>> + Send + Sync + 'static, )
Register an async streaming handler for POST requests.
Sourcepub fn get_sse(
&mut self,
path: &str,
handler: impl Fn(&Request, &mut SseWriter) -> Result<(), RuntimeError> + Send + Sync + 'static,
)
pub fn get_sse( &mut self, path: &str, handler: impl Fn(&Request, &mut SseWriter) -> Result<(), RuntimeError> + Send + Sync + 'static, )
Register an SSE streaming handler for GET requests.
The handler receives the request and an SseWriter for sending events.
The connection stays open until the handler returns or the client disconnects.
Sourcepub fn ws(
&mut self,
path: &str,
handler: impl Fn(&Request, WsConn) -> Result<(), RuntimeError> + Send + Sync + 'static,
)
pub fn ws( &mut self, path: &str, handler: impl Fn(&Request, WsConn) -> Result<(), RuntimeError> + Send + Sync + 'static, )
Register a WebSocket handler for the given path.
The handler receives the upgrade request and a bidirectional WsConn.
The connection stays open until the handler returns or the client disconnects.
Sourcepub fn grpc(&mut self, grpc_router: GrpcRouter)
pub fn grpc(&mut self, grpc_router: GrpcRouter)
Register a gRPC service (generated by camber-build).
Requests with content-type: application/grpc are forwarded to the
tonic service. All other requests go through normal HTTP routing.
Sourcepub fn proxy(&mut self, prefix: &str, backend: &str)
pub fn proxy(&mut self, prefix: &str, backend: &str)
Register a reverse proxy that forwards requests under prefix to backend.
The prefix is stripped from the request path before forwarding. All HTTP methods are handled. The full upstream response is buffered, so middleware can inspect and modify the response body. On backend failure, returns 502.
Sourcepub fn proxy_checked(
&mut self,
prefix: &str,
backend: &str,
healthy: Arc<AtomicBool>,
)
pub fn proxy_checked( &mut self, prefix: &str, backend: &str, healthy: Arc<AtomicBool>, )
Register a health-checked reverse proxy.
Behaves like proxy() but checks the healthy flag before forwarding.
When healthy is false, returns 503 immediately.
Sourcepub fn proxy_stream(&mut self, prefix: &str, backend: &str)
pub fn proxy_stream(&mut self, prefix: &str, backend: &str)
Register a streaming reverse proxy under prefix.
Like proxy(), but the upstream response body is forwarded chunk-by-chunk
with backpressure instead of being buffered in memory. Middleware acts as
a request gate only — it can reject before the upstream call, but does not
wrap the streamed response.
Sourcepub fn proxy_checked_stream(
&mut self,
prefix: &str,
backend: &str,
healthy: Arc<AtomicBool>,
)
pub fn proxy_checked_stream( &mut self, prefix: &str, backend: &str, healthy: Arc<AtomicBool>, )
Register a health-checked streaming reverse proxy.
Behaves like proxy_stream() but checks the healthy flag before forwarding.
When healthy is false, returns 503 immediately.
Sourcepub fn static_files(&mut self, prefix: &str, dir: &str)
pub fn static_files(&mut self, prefix: &str, dir: &str)
Serve static files from dir under the given URL prefix.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Router
impl !RefUnwindSafe for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Router
impl !UnwindSafe for Router
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request