cognee_http_server/
lib.rs1pub mod auth;
17pub mod auth_resolver;
18pub mod cloud_client;
19pub mod components;
20pub mod config;
21pub mod dto;
22pub mod error;
23pub mod health;
24pub mod lifecycle;
25pub mod middleware;
26pub mod multipart;
27pub mod notebook_runner;
28pub mod observability;
29pub mod openapi;
30pub mod permissions;
31pub mod pipelines;
32pub mod responses;
33pub mod responses_dispatch;
34mod router_builder;
35pub mod routers;
36pub mod state;
37pub mod sync;
38pub mod telemetry;
39pub mod wiring;
40
41pub use config::HttpServerConfig;
42pub use error::{ApiError, ServerError};
43pub use router_builder::{RouterBuilder, build_router};
44pub use state::AppState;
45
46use std::net::SocketAddr;
47
48#[cfg(feature = "bin")]
55async fn shutdown_signal(state: AppState) {
56 use tokio::signal;
57
58 let ctrl_c = async {
59 signal::ctrl_c()
60 .await
61 .expect("failed to install Ctrl+C handler");
62 };
63
64 #[cfg(unix)]
65 let terminate = async {
66 signal::unix::signal(signal::unix::SignalKind::terminate())
67 .expect("failed to install SIGTERM handler")
68 .recv()
69 .await;
70 };
71
72 #[cfg(not(unix))]
73 let terminate = std::future::pending::<()>();
74
75 tokio::select! {
76 () = ctrl_c => {}
77 () = terminate => {}
78 }
79
80 lifecycle::on_shutdown(&state).await;
81}
82
83pub async fn run(addr: SocketAddr, state: AppState) -> Result<(), ServerError> {
90 let app = build_router(state.clone()).await?;
91 let listener = tokio::net::TcpListener::bind(addr).await?;
92
93 tracing::info!("listening on {addr}");
94
95 #[cfg(feature = "bin")]
96 {
97 axum::serve(listener, app)
98 .with_graceful_shutdown(shutdown_signal(state))
99 .await
100 .map_err(|e| ServerError::Other(anyhow::anyhow!(e)))?;
101 }
102
103 #[cfg(not(feature = "bin"))]
104 {
105 axum::serve(listener, app)
108 .await
109 .map_err(|e| ServerError::Other(anyhow::anyhow!(e)))?;
110 }
111
112 Ok(())
113}