1mod api;
18
19use std::sync::Arc;
20
21use axum::{routing::get, Router};
22use crdt_store::SqliteStore;
23
24pub(crate) struct AppState {
26 pub store: SqliteStore,
27 pub db_path: String,
28}
29
30pub async fn start(db_path: &str, port: u16) -> Result<(), Box<dyn std::error::Error>> {
36 let store = SqliteStore::open(db_path)?;
37 let state = Arc::new(AppState {
38 store,
39 db_path: db_path.to_string(),
40 });
41
42 let app = Router::new()
43 .route("/", get(api::index))
44 .route("/api/status", get(api::status))
45 .route("/api/namespaces/{ns}/entities", get(api::list_entities))
46 .route("/api/namespaces/{ns}/entities/{key}", get(api::get_entity))
47 .route(
48 "/api/namespaces/{ns}/entities/{key}/events",
49 get(api::get_events),
50 )
51 .with_state(state);
52
53 let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}")).await?;
54 eprintln!(" Dev UI: http://localhost:{port}");
55 axum::serve(listener, app).await?;
56 Ok(())
57}