Skip to main content

Crate slabs

Crate slabs 

Source
Expand description

Batteries-included Transaction Script web framework in Rust.

§What this crate owns

  • Facade re-exports of every Slabs sub-crate: assets, auth, cache, core, db, html, http, jobs, mail, realtime, storage, test, and webhooks.
  • Top-level re-exports: id, AppConfig, AppEnv, AppManifest, JobMeta, RouteMeta, SlabError, SlabResult, build_router, client_ip, AppCtx, ClientIp, JobDef, JobRegistry, RateLimiter, ScheduleDef, SlabDef, PresenceEntry, PresenceGuard, validate_ws_origin, json_script, redirect_external, Flash, and FlashKind.
  • prelude: single import (use slabs::prelude::*;) for slab and job files.
  • discover_slabs!: macro that includes $OUT_DIR/discovered_slabs.rs generated by slabs-build.
  • SlabsApp: validates config, boots AppCtx, runs migrations, builds the router, and serves HTTP plus background workers.
  • init_tracing: initializes tracing_subscriber with RUST_LOG and optional JSON output.

§Agent invariants

  • Invoke slabs::discover_slabs!(); once in the application crate’s src/lib.rs. It generates discovered_slabs, discovered_jobs, collect_slabs(), and collect_jobs().
  • Import use slabs::prelude::*; inside slab files instead of wiring sub-crates individually.
  • Boot the app with SlabsApp::boot, passing a sqlx::migrate::Migrator, collect_slabs(), and collect_jobs().
  • Attach recurring jobs with SlabsApp::with_schedules before calling SlabsApp::serve.
  • SlabsApp::serve spawns background job workers and schedules, binds config.bind_addr, and shuts down cleanly on SIGINT or SIGTERM.
  • Call init_tracing at the top of main(). Set SLABS_LOG_FORMAT=json for structured JSON logs and RUST_LOG to override the default filter (info,tower_http=info,sqlx=warn).

§Example

use slabs::{AppConfig, SlabsApp};

// In src/lib.rs, after calling slabs_build::discover() in build.rs:
// slabs::discover_slabs!();
// pub static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("./migrations");

async fn run(migrator: &sqlx::migrate::Migrator) -> slabs::SlabResult<()> {
    slabs::init_tracing();
    let config = AppConfig::from_env("my_app");
    let app = SlabsApp::boot(config, migrator, vec![], vec![]).await?;
    app.serve().await
}

Re-exports§

pub use slabs_assets as assets;
pub use slabs_auth as auth;
pub use slabs_cache as cache;
pub use slabs_core as core;
pub use slabs_db as db;
pub use slabs_html as html;
pub use slabs_http as http;
pub use slabs_jobs as jobs;
pub use slabs_mail as mail;
pub use slabs_realtime as realtime;
pub use slabs_storage as storage;
pub use slabs_test as test;
pub use slabs_webhooks as webhooks;

Modules§

id
Prefix-tagged, time-sortable ULID generator (for example msg_01j... or usr_01j...).
position
Fractional list positioning helpers for O(1) single-row reordering.
prelude
Standard Slabs types, helpers, and maud HTML macros re-exported for slab and job files.
slug
URL slug generation helpers.

Macros§

discover_slabs
Include the compile-time discovered transaction scripts and background jobs generated by slabs_build::discover().

Structs§

AppConfig
Runtime configuration loaded from SLABS_* and DATABASE_URL environment variables.
AppCtx
Shared runtime context passed to every HTTP slab handler and background job.
AppManifest
Complete JSON manifest of an application’s discovered HTTP routes and background jobs.
AuditEntry
Persisted audit event row stored in slabs_audit_log.
AuditLog
Transactional audit log backed by the slabs_audit_log system table.
ClientIp
Axum request-parts extractor resolving the client’s IP address via client_ip.
CursorPage
One page of cursor-paginated results.
Flash
One-shot feedback message carried across a redirect in a signed cookie.
IncidentInput
Input payload for recording a deduplicated runtime incident in slabs_incidents.
IncidentRecord
Persisted runtime incident row stored in slabs_incidents.
Incidents
Deduplicated runtime incident store backed by the slabs_incidents system table.
JobDef
Self-contained Background Job Transaction Script definition returned by pub fn job() -> JobDef.
JobMeta
Machine-readable metadata describing a registered background job.
JobRegistry
Dispatches SQLite-queued jobs to discovered JobDef handlers.
Pagination
Query parameters for cursor-based (keyset) pagination.
PresenceEntry
Active presence entry tracked on a realtime topic.
PresenceGuard
Guard representing an active presence registration on a topic.
RateLimiter
Explicit in-memory fixed-window rate limiter for auth and sensitive slabs.
RouteMeta
Machine-readable HTTP route metadata emitted by /_slabs/manifest and slabs manifest.
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.
SlabsApp
High-level boot entry point for a Slabs application.
Validate
Multi-field procedural validation builder for transaction scripts.

Enums§

AppEnv
Deployment environment controlling security defaults and fail-closed startup checks.
FlashKind
Severity level for a one-shot Flash banner message.
IncidentOutcome
Lifecycle outcome returned when recording an incident via Incidents::record_with_outcome.
SlabError
Unified HTTP error type returned by every transaction script and background job.

Functions§

bug_report_widget
Render a toggleable, CSP-safe user bug reporter button and native <dialog> widget.
bug_report_widget_with_label
Render a toggleable, CSP-safe user bug reporter widget with a custom trigger button_label.
build_router
Assemble the full axum::Router from discovered slabs, built-in endpoints, and middleware.
client_ip
Resolve the client’s IP address for logging and rate-limit keys.
fingerprint
Compute a lowercase SHA-256 hex digest over length-prefixed string parts.
init_tracing
Initialize tracing_subscriber using RUST_LOG (defaulting to info,tower_http=info,sqlx=warn).
json_script
Serialize value as JSON safely embedded inside <script type="application/json" id="...">.
redirect_external
Emit an explicit 303 See Other redirect to an external http:// or https:// URL.
validate_ws_origin
Validate WebSocket upgrade request headers against Cross-Site WebSocket Hijacking (CSWSH).

Type Aliases§

SlabResult
Result alias using SlabError as the error type.