tidaldb/tidal/examples/actix_embedding.rs
jordan 6fdaa1584b feat: complete M1 signal engine — m0p3 samples/docs, m1p5 TidalDb API, examples, and periodic checkpoint
- m0p3: CONTRIBUTING.md with run-samples checklist, all 4 examples
  (quickstart, cli_embedding, axum_embedding, actix_embedding), doc-test
  coverage for every public API surface
- m1p5: TidalDb public API — write_item, signal, read_decay_score,
  read_windowed_count, read_velocity; StorageBox enum routing memory vs
  fjall; WalSender/WalHandleWriter bridge; WAL replay on open
- Periodic checkpoint: 30s background thread for persistent+schema mode;
  FjallBackend::Clone (O(1), fjall::Keyspace is ref-counted); graceful
  shutdown via Arc<AtomicBool> + join before final checkpoint
- ROADMAP.md: M0 and M1 fully marked COMPLETE (341 tests passing)
- Milestone 2 planning scaffolding added under docs/planning/milestone-2/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-20 22:45:10 -07:00

64 lines
1.9 KiB
Rust

//! tidalDB + Actix-web: embedding a tidalDB instance in an Actix-web server.
//!
//! Demonstrates:
//! - Wrapping `TidalDb` in `Arc` for shared ownership
//! - Passing the instance via `web::Data<Arc<TidalDb>>`
//! - A `/health` route that calls `health_check()`
//! - Graceful shutdown via Actix's built-in signal handling
//!
//! `TidalDb` is not `Clone`, so it is wrapped in `Arc`. `web::Data` then
//! wraps the `Arc`, giving each handler a cheap clone of the pointer.
//!
//! # Running
//!
//! ```bash
//! cargo run --example actix_embedding --manifest-path tidal/Cargo.toml
//! # Then: curl http://127.0.0.1:3001/health
//! ```
use std::sync::Arc;
use actix_web::{App, HttpResponse, HttpServer, web};
use tidaldb::TidalDb;
async fn health(db: web::Data<Arc<TidalDb>>) -> HttpResponse {
match db.health_check() {
Ok(()) => HttpResponse::Ok().body("ok"),
Err(_) => HttpResponse::ServiceUnavailable().body("degraded"),
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
tracing_subscriber::fmt()
.with_env_filter("tidaldb=debug")
.init();
// Open tidalDB; map LumenError -> io::Error so Actix's Result type aligns.
let db = Arc::new(
TidalDb::builder()
.ephemeral()
.open()
.map_err(std::io::Error::other)?,
);
// Wrap in web::Data so Actix can clone it cheaply into each worker thread.
let db_data = web::Data::new(Arc::clone(&db));
println!("listening on http://127.0.0.1:3001");
println!(" GET /health -> tidalDB health check");
println!("press Ctrl+C to stop");
HttpServer::new(move || {
App::new()
.app_data(db_data.clone())
.route("/health", web::get().to(health))
})
.bind("127.0.0.1:3001")?
.run()
.await?;
// `db` drops here, triggering TidalDb::drop() -> shutdown_inner().
Ok(())
}