servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! Deferred initialization and explicitly registered resource cleanup.

mod resources;

use crate::{
    Result,
    task::{self, Shutdown},
};
use axum::Router;
pub(crate) use resources::Resources;
use std::{future::Future, pin::Pin};
use tokio::time::Instant;

type SetupFuture =
    Pin<Box<dyn Future<Output = std::result::Result<Router, task::BoxError>> + Send>>;
pub(crate) type Factory = Box<dyn FnOnce(SetupContext) -> SetupFuture + Send>;

/// The startup deadline and ownership boundary for deferred initialization.
///
/// Explicitly registered resources outlive accepted work and application cleanup.
/// Unregistered resources remain application-owned. This is not a service locator.
/// The context cannot be cloned, and registration closes when setup ends.
pub struct SetupContext {
    pub(crate) resources: Resources,
    pub(crate) metrics: crate::metrics::Registry,
    pub(crate) deadline: Instant,
    pub(crate) shutdown: Shutdown,
}

impl SetupContext {
    /// The app's bounded metrics registry, also exposed by the admin `/metrics` route.
    /// Register stable definitions during setup and retain native instrument handles
    /// in state. New registrations fail after setup completes or fails.
    pub fn metrics(&self) -> crate::metrics::Registry {
        self.metrics.clone()
    }

    /// Absolute startup deadline shared with listeners and worker readiness.
    pub fn deadline(&self) -> Instant {
        self.deadline
    }

    /// Read-only notification that startup or the application is being stopped.
    pub fn shutdown(&self) -> Shutdown {
        self.shutdown.clone()
    }

    /// Register owned cleanup before exposing a custom resource to application code.
    ///
    /// Called after application cleanup, in reverse registration order, within the
    /// remaining shutdown budget. Registration is synchronous and bounded to 64
    /// unique names of at most 128 bytes. Names appear as `managed:<name>` in failures.
    /// A rejected registration does not take ownership of the resource; the caller
    /// must close it. Arbitrary resources are never discovered automatically.
    pub fn cleanup<F, Fut>(&mut self, name: impl Into<String>, factory: F) -> Result<()>
    where
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = task::Result> + Send + 'static,
    {
        self.resources
            .register(name.into(), Box::new(move || Box::pin(factory())))
    }
}

impl SetupContext {
    /// Register a cached dependency check during setup.
    ///
    /// Startup waits for its first success within the existing deadline.
    /// Readiness uses the latest result, which can fail before startup ends.
    /// Checks run serially with a timeout. Probes read cached state.
    /// The application supports 64 checks total across builder and setup. Duplicate
    /// names across both, including generated worker names, fail startup and roll
    /// back registered resources. Registration closes when setup ends.
    pub fn readiness<F, Fut>(
        &mut self,
        name: impl Into<String>,
        policy: crate::health::CheckPolicy,
        check: F,
    ) -> Result<()>
    where
        F: Fn() -> Fut + Send + 'static,
        Fut: Future<Output = task::Result> + Send + 'static,
    {
        self.resources
            .readiness(crate::health::Check::new(name.into(), policy, check))
    }
}