scouter_server/api/
route.rs

1use std::sync::Arc;
2
3use axum::{
4    routing::{get, post},
5    Router,
6};
7
8use crate::api::handler::{get_drift, health_check, insert_drift, insert_drift_profile};
9use crate::sql::postgres::PostgresClient;
10use axum::http::{
11    header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
12    Method,
13};
14use tower_http::cors::CorsLayer;
15
16pub struct AppState {
17    pub db: PostgresClient,
18}
19
20pub fn create_router(app_state: Arc<AppState>) -> Router {
21    let cors = CorsLayer::new()
22        .allow_methods([Method::GET, Method::PUT, Method::DELETE])
23        .allow_credentials(true)
24        .allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE]);
25
26    Router::new()
27        .route("/healthcheck", get(health_check))
28        .route("/drift", get(get_drift).post(insert_drift))
29        .route("/profile", post(insert_drift_profile))
30        .with_state(app_state)
31        .layer(cors)
32}