tidaldb/tidal/examples/actix_embedding.rs
jx12n 3bcfb3c576 feat: Bazel build, crate docs/ai-lookup, docker images, and engine hardening
- Add BUILD.bazel across tidal, tidal-net, tidal-server, tidalctl for bzlmod build
- Add tidal/ crate docs (README, CHANGELOG, CONTRIBUTING, AGENTS, CLAUDE, API, ARCHITECTURE) and ai-lookup reference
- Add docker standalone/cluster/deploy images, compose, and prometheus config
- Harden WAL (batch format, writer, dedup, diagnostics), text syncer/collectors, and vector registry
- Expand tidalctl CLI and tests; restructure WAL/visibility integration test suites
- Refine tidal-net transport/client/server and tidal-server cluster/scatter-gather
2026-06-07 18:29:38 -06:00

64 lines
1.8 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 -p tidaldb
//! # 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 TidalError -> 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(())
}