Skip to main content

AppBuilder

Struct AppBuilder 

Source
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

Source

pub fn host(self, host: impl Into<String>) -> Self

Set the bind host (default "127.0.0.1"). Returns self for chaining.

Source

pub fn port(self, port: u16) -> Self

Set the bind port (default 8080). Returns self for chaining.

Source

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.

Source

pub fn with_config(self, cfg: Config) -> Self

Apply a fully-resolved Config, overwriting the current server settings.

This is lowest precedence: DSL setters called after it (e.g. another port) win. Use it to seed the builder from a config file/env, then override specific fields in code. Returns self for chaining.

Source

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.

Source

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.

Source

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");
Source

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.

Source

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.

Source

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.

Source

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");
Source

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
Source

pub fn build(self) -> App

Finish building into an immutable, cheaply-cloneable App.

Installed middleware is sorted by Phase (stably, preserving install order within a phase) and the configuration is frozen.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more