saluki_app/
config.rs

1//! Configuration API handler.
2
3use http::StatusCode;
4use saluki_api::{
5    extract::State,
6    response::IntoResponse,
7    routing::{get, Router},
8    APIHandler,
9};
10use saluki_config::GenericConfiguration;
11use serde_json::Value;
12
13/// State for the configuration API handler.
14#[derive(Clone)]
15pub struct ConfigState {
16    config: GenericConfiguration,
17}
18
19/// An API handler for exposing the current configuration.
20pub struct ConfigAPIHandler {
21    state: ConfigState,
22}
23
24impl ConfigAPIHandler {
25    /// Creates a new `ConfigAPIHandler`.
26    pub fn new(config: GenericConfiguration) -> Self {
27        Self {
28            state: ConfigState { config },
29        }
30    }
31
32    async fn config_handler(State(state): State<ConfigState>) -> impl IntoResponse {
33        match state.config.as_typed::<Value>() {
34            Ok(config) => (StatusCode::OK, serde_json::to_string(&config).unwrap()).into_response(),
35            Err(e) => (
36                StatusCode::INTERNAL_SERVER_ERROR,
37                format!("Failed to get configuration: {}", e),
38            )
39                .into_response(),
40        }
41    }
42}
43
44impl APIHandler for ConfigAPIHandler {
45    type State = ConfigState;
46
47    fn generate_initial_state(&self) -> Self::State {
48        self.state.clone()
49    }
50
51    fn generate_routes(&self) -> Router<Self::State> {
52        Router::new().route("/config", get(Self::config_handler))
53    }
54}