pub struct AppWithState<S> { /* private fields */ }Expand description
An Application that combines user-defined state-aware routes with the
built-in App controller chain as a fallback.
Routes are matched in registration order. The first match wins; unmatched
requests are forwarded to App (static files, health probes, etc.).
Implementations§
Source§impl<S: Send + Sync + 'static> AppWithState<S>
impl<S: Send + Sync + 'static> AppWithState<S>
Sourcepub fn new(state: S) -> Self
pub fn new(state: S) -> Self
Create a new AppWithState wrapping state.
state is stored behind an Arc so it can be shared across threads
without cloning. Register routes with the builder methods.
Sourcepub fn with_config(self, config: ServerConfig) -> Self
pub fn with_config(self, config: ServerConfig) -> Self
Pin the fallback App (used for any request this app’s own routes
don’t match) to an explicit ServerConfig, instead of reading
RWS_CONFIG_* environment variables per request.
Mirrors App::with_config — same rationale: safe for parallel
tests (no test_env::lock() needed) and lets multiple
differently-configured instances coexist in one process.
Sourcepub fn delete<F>(self, pattern: &str, handler: F) -> Self
pub fn delete<F>(self, pattern: &str, handler: F) -> Self
Register a DELETE handler for pattern.
Sourcepub fn route_entries(&self) -> Vec<RouteInfo>
pub fn route_entries(&self) -> Vec<RouteInfo>
Return a snapshot of all registered routes as (method, pattern) pairs.
Sourcepub fn mcp(
self,
name: impl Into<String>,
version: impl Into<String>,
) -> McpServer
pub fn mcp( self, name: impl Into<String>, version: impl Into<String>, ) -> McpServer
Attach an MCP server to this application. Requests that do not match
the MCP endpoint (POST /mcp) are forwarded to self, so all
previously registered routes remain active.
use rust_web_server::app::App;
use rust_web_server::mcp::{McpContent, extract_arg};
use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
use rust_web_server::core::New;
struct Db { url: String }
let app = App::with_state(Db { url: "postgres://localhost/mydb".to_string() })
.get("/api/users", |_req, _params, _conn, _db| {
let mut r = Response::new();
r.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
r.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
r
})
.mcp("my-server", "1.0")
.tool("list_users", "List all users", "{}", |_| {
Ok(McpContent::json(r#"[{"id":1,"name":"Alice"}]"#))
});Sourcepub fn wrap<M: Middleware + 'static>(
self,
layer: M,
) -> WithMiddleware<AppWithState<S>>
pub fn wrap<M: Middleware + 'static>( self, layer: M, ) -> WithMiddleware<AppWithState<S>>
Wrap this application in a middleware layer.
Enables fluent composition:
use rust_web_server::app::App;
use rust_web_server::core::New;
use rust_web_server::middleware::RateLimitLayer;
use rust_web_server::response::Response;
let app = App::with_state(())
.get("/ping", |_, _, _, _| Response::new())
.wrap(RateLimitLayer);Trait Implementations§
Source§impl<S: Send + Sync + 'static> Application for AppWithState<S>
impl<S: Send + Sync + 'static> Application for AppWithState<S>
Source§impl<S: Clone> Clone for AppWithState<S>
impl<S: Clone> Clone for AppWithState<S>
Source§fn clone(&self) -> AppWithState<S>
fn clone(&self) -> AppWithState<S>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more