Skip to main content

ServerBuilder

Struct ServerBuilder 

Source
pub struct ServerBuilder { /* private fields */ }
Expand description

Builder for Server.

Individual setters override values from config().

Implementations§

Source§

impl ServerBuilder

Source

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

Bulk-applies a loaded Config (e.g. from nitr.toml). Setters called afterwards override it.

Source

pub fn listen(self, addr: SocketAddr) -> Self

Address the server binds to.

Source

pub fn listener(self, listener: TcpListener) -> Self

Serves on an already-bound listener instead of binding listen.

This closes the window between choosing a port and binding it: a caller can bind port 0 (the OS picks a free one), read the real address, and hand the listener over — nothing else can take the port in between. That makes it the right tool for tests and for socket-activation setups where the supervisor owns the socket.

Source

pub fn health_listener(self, listener: TcpListener) -> Self

Serves the health probes on an already-bound listener instead of binding [health] bind.

The probe-port counterpart of listener, closing the same choose-then-bind window: reserve port 0, read the real address, hand the listener over. Ignored when [health] enabled is off.

Source

pub fn handler_script(self, path: impl Into<PathBuf>) -> Self

The handler script, loaded once per Lua state at startup and on every reload; its routes serve the requests.

Source

pub fn config_script(self, path: impl Into<PathBuf>) -> Self

Lua script executed exactly once at startup; its returned table is passed to the handler on every request.

Source

pub fn templates_dir(self, path: impl Into<PathBuf>) -> Self

Directory for the template builtin ([templating] dir).

Source

pub fn database(self, path: impl Into<PathBuf>) -> Self

SQLite database file for the conn builtin.

Source

pub fn builtins(self, builtins: Builtins) -> Self

Standard library (nitr.*) features to expose; overrides the [std] features list from the configuration file. Without either, the minimal default set (Builtins::minimal()) is enabled.

Source

pub fn workers(self, n: usize) -> Self

Number of pooled Lua states (max concurrently executing handlers).

Source

pub fn dev_mode(self, on: bool) -> Self

Development mode: hot-reload the handler script on change.

Source

pub fn module<F>(self, name: impl Into<String>, f: F) -> Self
where F: Fn(&Lua) -> Result<Table> + Send + Sync + 'static,

Registers a Rust extension module: the closure runs once per pooled state (and again on every reload) and its returned table is mounted at nitr.ext.<name> — one level below the standard library, so a module can never collide with a builtin (present or future). Registration fails at build time when two modules share a name.

ⓘ
// Scripts call it as nitr.ext.greet.hello("world").
Server::builder().module("greet", |lua| {
    let t = lua.create_table()?;
    t.set("hello", lua.create_function(|_, name: String| {
        Ok(format!("Hello, {name}!"))
    })?)?;
    Ok(t)
})
Source

pub fn cache(self, cache: Cache) -> Self

The storage behind nitr.cache, instead of a fresh one sized by [cache]. nitr test hands its test states the same instance, so what a handler caches is observable from a test. Unused when the cache builtin is not enabled.

Source

pub fn setup<F>(self, f: F) -> Self
where F: Fn(&Lua) -> Result<()> + Send + Sync + 'static,

Registers a closure that customizes each pooled Lua state — the low-level escape hatch behind module(). It runs once per state, before the configuration script and handler are loaded.

Source

pub async fn build(self) -> Result<Server>

Builds the server: creates the runtime pool, runs the configuration script exactly once, snapshots its result into every state, and compiles the handler. Fails fast on any configuration or script error.

Trait Implementations§

Source§

impl Default for ServerBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSync for T
where T: Sync,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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<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