Expand description
Async-capable state-aware application — requires the http2 feature (tokio).
AsyncAppWithState<S> is the async counterpart to AppWithState<S>:
handlers are async fn closures that can await database queries, HTTP
clients, or any other async I/O without blocking the OS thread.
The sync Application bridge works in any calling context: when inside
an existing tokio runtime (HTTP/2 / HTTP/3), it spawns a scoped OS thread
with its own single-threaded runtime; when called from the HTTP/1.1
thread-pool (no runtime), it creates a temporary single-threaded runtime.
Unmatched routes fall through to the built-in App controller chain.
§Example
use std::sync::Arc;
use rust_web_server::async_state::AsyncAppWithState;
use rust_web_server::core::New;
use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
use rust_web_server::range::Range;
use rust_web_server::mime_type::MimeType;
use rust_web_server::router::PathParams;
use rust_web_server::request::Request;
use rust_web_server::server::ConnectionInfo;
struct AppState {
greeting: String,
}
let app = AsyncAppWithState::new(AppState { greeting: "Hello".to_string() })
.get("/greet/:name", |_req, params, _conn, state| async move {
let name = params.get("name").unwrap_or("world");
let body = format!("{}, {}!", state.greeting, name);
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.content_range_list = vec![
Range::get_content_range(body.into_bytes(), MimeType::TEXT_PLAIN.to_string())
];
r
});Structs§
- Async
AppWith State - An
Applicationwhose route handlers areasyncfunctions.