servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
<p align="center">
  <img src="docs/assets/servlet-banner.png" alt="Servlet">
</p>

<p align="center">
  <strong>A Rust server toolkit for Axum and Tokio.</strong><br>
  Run HTTP APIs and background tasks with readiness checks and graceful shutdown.
</p>

<p align="center">
  <a href="https://docs.rs/servlet">API docs</a> ·
  <a href="https://crates.io/crates/servlet">crates.io</a> ·
  <a href="https://github.com/chimlet/servlet/releases">Releases</a>
</p>

## Quick start

Create a project with Rust 1.97 or later.

```sh
cargo new hello-servlet
cd hello-servlet
cargo add servlet@1.2.0
```

Save this as `src/main.rs`.

```rust
use servlet::{App, Result};

#[servlet::main]
async fn main() -> Result<()> {
    App::new("hello-servlet")
        .get("/", async || "hello\n")
        .run()
        .await
}
```

Start the server with `cargo run`, then call it from another terminal.

```console
$ curl http://127.0.0.1:8080/
hello
```

Use `App::http` to pass in an existing Axum router.

## Roles and components

Use `App::with_role` to choose what a process runs. For a binary with several named components, register factories with `Components::component` and pick one with `Components::select`.

| Role | Application HTTP | `App::worker` | `App::service` |
| --- | --- | --- | --- |
| `All` (default) | Yes | Yes | Yes |
| `Api` | Yes | No | Yes |
| `Worker` | No | Yes | Yes |

Services and workers start after setup and call `ctx.ready()` when ready. The configured admin listener stays available in every role. Use `AppConfig::from_env_for_role` when loading settings explicitly.

Register `worker_with_state` and `service_with_state` before `setup` or `state`. A selected task receives a clone of the initialized state.

```rust
use servlet::{App, Result};

#[servlet::main]
async fn main() -> Result<()> {
    App::<String>::new("background")
        .worker_with_state("report", |message, ctx| async move {
            println!("{message}");
            ctx.ready();
            ctx.shutdown().cancelled().await;
            Ok(())
        })
        .setup(|_| async { Ok(String::from("worker initialized")) })
        .run()
        .await
}
```

For recurring work, `WorkerContext::poll` handles readiness, waits, shutdown, and optional progress tracking.

## Health and metrics

The app listens on `127.0.0.1:8080` by default. Set `SERVLET_BIND` to change its address.

Set `SERVLET_ADMIN_BIND=127.0.0.1:9090` for a separate admin listener. It serves `/live`, `/ready`, and Prometheus metrics at `/metrics`.

Use `App::configure` to override selected settings after `SERVLET_*` variables load.

```rust
let app = servlet::App::<()>::new("example").configure(|config| {
    config.max_in_flight = 128;
});
```

Use `App::config` to supply the entire configuration and bypass automatic environment loading. For explicit environment loading, use `AppConfig::from_env_for_role`.

`SetupContext::status` gives application routes read-only lifecycle and readiness information. For a finite command, use `App::run_once` to run setup and await managed cleanup without opening listeners.

## Optional features

| Feature | Adds |
| --- | --- |
| `client` | Pooled HTTP requests with deadlines and body limits |
| `postgres` | SQLx connection pools, readiness checks, and managed listeners |
| `otel` | OpenTelemetry trace export over OTLP/HTTP |
| `recourse` | Typed HTTP errors with request IDs |

Open the API reference from the repository root.

```sh
cargo doc --locked -p servlet \
  --all-features --no-deps --open
```

## Test an HTTP app

Let the OS choose a free port and await startup before sending requests.

```rust
let mut config = servlet::AppConfig::default();
config.bind = "127.0.0.1:0".parse()?;
let running = servlet::App::new("fixture")
    .config(config)
    .get("/", || async { "hello" })
    .start()
    .await?;
let address = running.local_addr().expect("fixture listener");
// Send test requests to address.
running.handle().shutdown();
running.wait().await?;
```

## Dependency versions

Servlet 1.2.0 declares these requirements in its manifest. The release lockfile records the exact versions tested.

| Crate | Cargo requirement | Tested version |
| --- | --- | --- |
| `axum` | `0.8` | `0.8.9` |
| `tokio` | `1.53.3` | `1.54.1` |
| `tokio-util` | `0.7` | `0.7.20` |
| `hyper` | `1` | `1.12.0` |
| `hyper-util` | `0.1` | `0.1.21` |
| `tower` | `0.5` | `0.5.3` |
| `tracing` | `0.1` | `0.1.44` |
| `tracing-subscriber` | `0.3` | `0.3.23` |
| `prometheus` | `0.14` | `0.14.0` |
| `reqwest` | `0.13` | `0.13.5` |
| `sqlx` | `=0.9.1` | `0.9.1` |
| `tokio-postgres` | `0.7.18` | `0.7.19` |
| `tokio-postgres-rustls` | `0.14` | `0.14.0` |
| `opentelemetry` | `0.33` | `0.33.1` |
| `recourse` | `=0.0.1-rc.3` | `0.0.1-rc.3` |
| `recourse-axum` | `=0.0.1-rc.3` | `0.0.1-rc.3` |
| `zlid` | `=0.0.1-rc.4` | `0.0.1-rc.4` |

## License

[MIT](LICENSE-MIT) or [Apache 2.0](LICENSE-APACHE).