pub struct App { /* private fields */ }Expand description
Main application struct
Implementations§
Source§impl App
impl App
pub fn new() -> Self
Sourcepub fn max_body_size(self, size: Option<usize>) -> Self
pub fn max_body_size(self, size: Option<usize>) -> Self
Set maximum request body size in bytes (None = unlimited)
Sourcepub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
pub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
Register shared application state, readable in handlers and
middleware with Request::state
One value per type; registering the same type again replaces it. Use
interior mutability (Mutex, RwLock, atomics) for mutable state: the
value is shared by all requests and is not cloned.
use ullun::prelude::*;
use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Default)]
struct Hits(AtomicU64);
async fn count(req: Request) -> Result<Response> {
let hits = req.state::<Hits>()?.0.fetch_add(1, Ordering::Relaxed) + 1;
Ok(Response::text(hits.to_string()))
}
let app = App::new().state(Hits::default()).get("/", count);Sourcepub fn group(
self,
prefix: &str,
configure: impl FnOnce(RouteGroup) -> RouteGroup,
) -> Self
pub fn group( self, prefix: &str, configure: impl FnOnce(RouteGroup) -> RouteGroup, ) -> Self
Create a route group with a common prefix
Sourcepub fn doc(self, method: &str, path: &str, operation: Operation) -> Self
pub fn doc(self, method: &str, path: &str, operation: Operation) -> Self
Describe a registered route for the OpenAPI document
path is the path exactly as registered, e.g. /users/{id}.
Sourcepub fn openapi(self, info: OpenApiInfo) -> Self
pub fn openapi(self, info: OpenApiInfo) -> Self
Publish an OpenAPI 3.1 document of all routes (and optionally Swagger UI)
See the openapi module.
Sourcepub fn get_with_params<F, Fut>(self, path: &str, handler: F) -> Self
pub fn get_with_params<F, Fut>(self, path: &str, handler: F) -> Self
Add GET route with path params extractor
Sourcepub fn ws<F, Fut>(self, path: &str, handler: F) -> Self
pub fn ws<F, Fut>(self, path: &str, handler: F) -> Self
Accept WebSocket connections on path (feature websocket)
The handler receives the original request (path params, query, headers, extensions set by middleware) and the upgraded socket, and runs on its own task until it returns.
Sourcepub fn ws_with<F, Fut>(self, path: &str, options: WsOptions, handler: F) -> Self
pub fn ws_with<F, Fut>(self, path: &str, options: WsOptions, handler: F) -> Self
Like App::ws with explicit size limit and allowed origins
Sourcepub fn middleware<M>(self, middleware: M) -> Selfwhere
M: Middleware,
pub fn middleware<M>(self, middleware: M) -> Selfwhere
M: Middleware,
Add middleware
Sourcepub fn serve_static(self, route_prefix: &str, dir_path: &str) -> Self
pub fn serve_static(self, route_prefix: &str, dir_path: &str) -> Self
Serve static files from a directory
Sourcepub async fn run(self, addr: &str) -> Result<()>
pub async fn run(self, addr: &str) -> Result<()>
Run the server with graceful shutdown on CTRL+C
Sourcepub async fn serve(self, listener: TcpListener) -> Result<()>
pub async fn serve(self, listener: TcpListener) -> Result<()>
Serve on an already bound listener until the future is dropped.
Useful for tests, benchmarks and embedding: bind to port 0, read the
assigned port from listener.local_addr(), then call serve.
Unlike App::run, this does not install a CTRL+C handler.