Skip to main content

Crate slabs_http

Crate slabs_http 

Source
Expand description

HTTP router assembly, shared application context, background job dispatch, rate limiting, and built-in endpoints for Slabs.

§What this crate owns

  • Types: AppCtx, SlabDef, JobDef, JobHandlerFn, ScheduleDef, JobRegistry, and RateLimiter.
  • Router builder: build_router, which combines discovered slabs, built-in endpoints, and the Tower middleware stack into an axum::Router.
  • Built-in routes mounted automatically by build_router:
    • GET /_slabs/health: pings the database with SELECT 1 and returns {"status": "ok", "app": "<app_name>"}.
    • GET /_slabs/manifest: returns the JSON slabs_core::AppManifest listing all registered routes and jobs.
    • GET /_slabs/openapi.json: returns an OpenAPI 3.1.0 JSON document generated from the application manifest and optional slab JSON schemas.
    • GET /_slabs/mail: returns up to 50 recent emails from the development outbox ({"emails": [...]}).
    • GET /_slabs/vendor/{*path}: serves embedded basecoat.min.css, basecoat.min.js, htmx.min.js, htmx-ext-sse.js, idiomorph-ext.min.js, and alpine.min.js with Cache-Control: public, max-age=86400.
    • GET /assets/{hash}/{*path}: serves content-hashed static files from ctx.assets with Cache-Control: public, max-age=31536000, immutable.
  • Middleware stack order in build_router (outermost to innermost on incoming requests):
    1. SetRequestIdLayer: assigns a time-sortable ULID X-Request-Id (req_<ulid>) when absent.
    2. TraceLayer: records HTTP request and response tracing spans.
    3. PropagateRequestIdLayer: copies X-Request-Id onto the response.
    4. CorsLayer::permissive(): sets permissive CORS headers.
    5. SetResponseHeaderLayer: sets X-Frame-Options: SAMEORIGIN if absent.
    6. SetResponseHeaderLayer: sets X-Content-Type-Options: nosniff if absent.
    7. RequestBodyLimitLayer: limits request bodies to 25 MiB.
    8. CompressionLayer: compresses responses with gzip and brotli (br).

§Agent invariants

§Example

use axum::{extract::{Path, State}, Json};
use serde_json::{json, Value};
use slabs_core::SlabResult;
use slabs_http::{build_router, AppCtx, SlabDef};

pub fn slab() -> SlabDef {
    SlabDef::post("/api/rooms/{room_id}/messages", handle)
        .name("messages.create")
        .summary("Post a message to a room")
}

async fn handle(
    State(ctx): State<AppCtx>,
    Path(room_id): Path<String>,
) -> SlabResult<Json<Value>> {
    ctx.rate_limit.check(&format!("room_msg:{room_id}"), 30, 60)?;
    Ok(Json(json!({ "room_id": room_id })))
}

fn assemble(ctx: AppCtx) -> axum::Router {
    build_router(ctx, vec![slab()], &[])
}

Structs§

AppCtx
Shared runtime context passed to every HTTP slab handler and background job.
JobDef
Self-contained Background Job Transaction Script definition returned by pub fn job() -> JobDef.
JobRegistry
Dispatches SQLite-queued jobs to discovered JobDef handlers.
RateLimiter
Explicit in-memory fixed-window rate limiter for auth and sensitive slabs.
ScheduleDef
Recurring maintenance schedule that enqueues a registered job on a fixed interval.
SlabDef
Self-contained HTTP Transaction Script definition returned by pub fn slab() -> SlabDef.

Functions§

build_router
Assemble the full axum::Router from discovered slabs, built-in endpoints, and middleware.

Type Aliases§

JobHandlerFn
Type-erased async handler function stored in a JobDef.