Skip to main content

orca_control/
lib.rs

1pub mod api;
2pub mod auth;
3pub mod cluster_api;
4pub(crate) mod cluster_handlers;
5pub mod cluster_state;
6pub mod deploy_history;
7pub mod health;
8pub(crate) mod operations;
9pub mod proto;
10pub mod raft;
11pub mod reconciler;
12pub(crate) mod routes;
13pub mod scheduler;
14pub mod state;
15pub mod store;
16pub mod webhook;
17
18use std::sync::Arc;
19
20use orca_core::config::ClusterConfig;
21use orca_core::runtime::Runtime;
22use tracing::info;
23
24use crate::state::{AppState, SharedRouteTable, SharedWasmTriggers};
25
26/// Start the orca control plane (API server).
27///
28/// # Errors
29///
30/// Returns an error if the server fails to bind or encounters a fatal error.
31pub async fn run_server(
32    cluster_config: ClusterConfig,
33    container_runtime: Arc<dyn Runtime>,
34    wasm_runtime: Option<Arc<dyn Runtime>>,
35    route_table: SharedRouteTable,
36    wasm_triggers: SharedWasmTriggers,
37) -> anyhow::Result<()> {
38    let state = Arc::new(AppState::new(
39        cluster_config.clone(),
40        container_runtime,
41        wasm_runtime,
42        route_table,
43        wasm_triggers,
44    ));
45    let app = api::router(state.clone());
46
47    let addr = format!("0.0.0.0:{}", cluster_config.cluster.api_port);
48    let listener = tokio::net::TcpListener::bind(&addr).await?;
49    info!("API server listening on {addr}");
50
51    axum::serve(listener, app)
52        .with_graceful_shutdown(shutdown_signal())
53        .await?;
54
55    Ok(())
56}
57
58async fn shutdown_signal() {
59    tokio::signal::ctrl_c()
60        .await
61        .expect("failed to install ctrl+c handler");
62    info!("Shutdown signal received");
63}