pub struct AppBuilder { /* private fields */ }Expand description
The fluent builder for an application, returned by Churust::server.
Chain DSL methods to configure the server (host,
port, tls, …), register shared
state, install plugins, and
define routes with routing. Finish with
build to get an App, or
start to build and serve in one step. DSL setters take
precedence over a with_config applied earlier.
use churust_core::{Churust, Call, TestClient};
let app = Churust::server()
.port(3000)
.routing(|r| { r.get("/", |_c: Call| async { "hi" }); })
.build();
assert_eq!(app.config().port, 3000);
let res = TestClient::new(app).get("/").send().await;
assert_eq!(res.text(), "hi");Implementations§
Source§impl AppBuilder
impl AppBuilder
Sourcepub fn host(self, host: impl Into<String>) -> Self
pub fn host(self, host: impl Into<String>) -> Self
Set the bind host (default "127.0.0.1"). Returns self for chaining.
Sourcepub fn port(self, port: u16) -> Self
pub fn port(self, port: u16) -> Self
Set the bind port (default 8080). Returns self for chaining.
Sourcepub fn max_body_bytes(self, n: usize) -> Self
pub fn max_body_bytes(self, n: usize) -> Self
Set the maximum accepted request body size in bytes (default 1 MiB).
Larger bodies are rejected with 413 Payload Too Large. Returns self
for chaining.
Sourcepub fn with_config(self, cfg: Config) -> Self
pub fn with_config(self, cfg: Config) -> Self
Sourcepub fn request_timeout_ms(self, ms: u64) -> Self
pub fn request_timeout_ms(self, ms: u64) -> Self
Set the per-request timeout in milliseconds (default 30000). A value of
0 disables the timeout. Requests exceeding it get 408 Request Timeout.
Returns self for chaining.
Sourcepub fn tls(
self,
cert_path: impl Into<String>,
key_path: impl Into<String>,
) -> Self
pub fn tls( self, cert_path: impl Into<String>, key_path: impl Into<String>, ) -> Self
Enable TLS, reading the certificate chain from cert_path and the
private key from key_path (PEM). The files are loaded when the server
starts; this only records the paths. Requires the tls feature to have
any effect at serve time. Returns self for chaining.
Sourcepub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
pub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
Register a shared application-state value of type T, retrieved later
via the State extractor or
Call::state. One value is held per type;
registering another T replaces it. Returns self for chaining.
use churust_core::{Churust, State, TestClient};
#[derive(Clone)]
struct Db { name: &'static str }
let app = Churust::server()
.state(Db { name: "postgres" })
.routing(|r| { r.get("/", |db: State<Db>| async move { db.name }); })
.build();
assert_eq!(TestClient::new(app).get("/").send().await.text(), "postgres");Sourcepub fn install<P: Plugin + 'static>(self, plugin: P) -> Self
pub fn install<P: Plugin + 'static>(self, plugin: P) -> Self
Install a Plugin, letting it register middleware/state. Consumes the
plugin value. Returns self for chaining.
Sourcepub fn add_middleware_in(&mut self, phase: Phase, mw: Arc<dyn Middleware>)
pub fn add_middleware_in(&mut self, phase: Phase, mw: Arc<dyn Middleware>)
Register a Middleware in a specific Phase. Plugins use this to
place their middleware precisely; the builder sorts all middleware by
phase (stably) at build time.
Sourcepub fn add_middleware(&mut self, mw: Arc<dyn Middleware>)
pub fn add_middleware(&mut self, mw: Arc<dyn Middleware>)
Register a Middleware in the default Phase::Plugins phase — the
common case for application middleware.
Sourcepub fn routing(self, f: impl FnOnce(&mut RouteBuilder<'_>)) -> Self
pub fn routing(self, f: impl FnOnce(&mut RouteBuilder<'_>)) -> Self
Define routes via the RouteBuilder DSL. The closure receives a
mutable builder on which to register handlers and nested scopes. Returns
self for chaining.
use churust_core::{Churust, Call, TestClient};
let app = Churust::server()
.routing(|r| {
r.get("/", |_c: Call| async { "home" });
r.post("/echo", |mut c: Call| async move { c.receive_text().await.unwrap_or_default() });
})
.build();
assert_eq!(TestClient::new(app).get("/").send().await.text(), "home");Sourcepub async fn start(self) -> Result<()>
pub async fn start(self) -> Result<()>
Build the app and serve it until Ctrl-C — a shorthand for
self.build().start().await. Binds a socket, so it does not return until
shutdown.
use churust_core::{Churust, Call};
Churust::server()
.routing(|r| { r.get("/", |_c: Call| async { "hi" }); })
.start()
.await