time_scheduler_server/
lib.rs

1#![deny(clippy::unwrap_used, clippy::expect_used)]
2
3use app::{AppData, AppState};
4use axum::{
5    middleware::from_fn_with_state,
6    routing::{get, post},
7    Extension, Router,
8};
9use std::path::PathBuf;
10use tokio::net::TcpListener;
11
12mod analysis;
13mod app;
14mod auth;
15mod blocktype;
16mod currentblock;
17mod err;
18mod handlers;
19mod timeblock;
20
21pub async fn run(
22    port: u16,
23    password_hash: String,
24    data_dir: PathBuf,
25) -> Result<(), Box<dyn std::error::Error>> {
26    let ip = format!("0.0.0.0:{}", port);
27    let state = AppState::init(password_hash).await;
28    let data = AppData::init(data_dir).await;
29
30    let routes = Router::new()
31        // Main home state for today
32        .route("/state", get(handlers::get_entire_state))
33        // Block types
34        .route("/blocktype/get", get(handlers::get_blocktypes))
35        .route("/blocktype/new", post(handlers::new_blocktype))
36        // Time blocks
37        .route("/timeblock/get", get(handlers::get_daydata))
38        .route("/timeblock/next", post(handlers::next_timeblock))
39        .route("/timeblock/split", post(handlers::split_timeblock))
40        .route("/timeblock/adjust", post(handlers::adjust_timeblock))
41        // Current block
42        .route("/currentblock/get", get(handlers::get_current_block))
43        .route("/currentblock/change", post(handlers::change_current_block))
44        // Analysis
45        .route("/analysis", get(handlers::get_analysis))
46        .layer(from_fn_with_state(
47            state.clone(),
48            auth::middleware::auth_middleware,
49        ))
50        // Auth
51        .route("/auth/login", post(auth::handlers::login))
52        .route("/auth/refresh", post(auth::handlers::refresh_token))
53        .route("/auth/check", post(auth::handlers::check_token))
54        .layer(Extension(state.clone()))
55        .with_state(data);
56
57    let listener = TcpListener::bind(&ip).await?;
58    axum::serve(listener, routes).await?;
59    Ok(())
60}