Skip to main content

Server

Struct Server 

Source
#[non_exhaustive]
pub struct Server { /* private fields */ }
Expand description

The central application server builder for Rullst.

Configures and boots the Axum HTTP server, optional ORM connection pool, task scheduler, hot-reload DLL watcher, traffic shield, and rate limiter in a single fluent chain.

§Example

use rullst_core::{Server, routes, routing::get};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    Server::new(routes![get("/" => || async { "OK" })])
        .run(3000)
        .await?;
    Ok(())
}

Implementations§

Source§

impl Server

Source

pub fn new(router: Router) -> Self

Creates a new Server from an already-built Router. Use Server::new_hot instead to enable hot-reload mode.

Source

pub fn new_hot<S: Into<String>>(lib_path: S) -> Self

Creates a Server in hot-reload mode that loads the application router from a compiled cdylib dynamic library at the given lib_path. The background file-watcher recompiles and hot-swaps the router on source changes.

Source

pub fn with_machine_endpoints(self, policy: MachineEndpointPolicy) -> Self

Requires explicit machine authentication before CSRF exemptions on exact routes.

Source

pub fn with_db<S: Into<String>>(self, db_url: S) -> Self

Sets a database URL to initialize the ORM connection pool at startup.

This requires the orm feature. When Core is compiled without orm, configuring a database remains a valid builder operation but Self::run fails closed with ServerError::Database.

Source

pub fn schedule(self, scheduler: Scheduler) -> Self

Attach a task scheduler that runs alongside the HTTP server.

The server owns the scheduler handle: every task failure is logged as a tracing error on the rullst::scheduler target when it is reported, and task failures never make a clean shutdown fail. Only a failed scheduler loop is returned as ServerError::Scheduler.

§Example
use rullst_core::{Server, Scheduler, routes, routing::get};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let scheduler = Scheduler::new()
        .task("0 0 * * *", || async { println!("daily cleanup"); })?;
    let router = routes![get("/" => || async { "OK" })];

    Server::new(router)
        .schedule(scheduler)
        .run(3000)
        .await?;
    Ok(())
}
Source

pub fn shield(self, shield: TrafficShield) -> Self

Attaches an adaptive TrafficShield to the server to protect against CPU/DB saturation.

Exact GET/HEAD /health and /ready probes are never shed.

Source

pub fn rate_limit(self, limiter: RateLimiter) -> Self

Attaches a global RateLimiter to the server.

Exact GET/HEAD /health and /ready probes do not consume tokens.

Source

pub fn with_lifecycle(self, lifecycle: ApplicationLifecycle) -> Self

Attaches a shared readiness and graceful-drain coordinator.

Static and development hot-reload requests are admitted only while this lifecycle and all of its required components are ready. Exact health probes remain reachable. Mount crate::health::health_router_with_lifecycle with a clone to expose the same aggregate state to an orchestrator.

Source

pub async fn run(self, port: u16) -> Result<(), ServerError>

Start the HTTP server on the specified port

Source

pub async fn run_with_shutdown<F>( self, port: u16, shutdown: F, ) -> Result<(), ServerError>
where F: Future<Output = ()> + Send + 'static,

Starts the HTTP server with a caller-supplied graceful-shutdown future.

This is useful for embedded process supervisors and deterministic tests. Resolving the future begins lifecycle draining before Axum waits for already accepted requests. Self::run remains the OS-signal default.

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<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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

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