tty_web/web/
mod.rs

1//! HTTP and WebSocket server built on [Axum](https://docs.rs/axum).
2//!
3//! Routes:
4//! - `GET /ws` — WebSocket endpoint (terminal I/O)
5//! - `GET /api/v1/ping` — health check
6//! - `GET /` and `GET /*path` — embedded static frontend
7
8mod health;
9mod static_files;
10mod ws;
11
12use std::path::PathBuf;
13use std::sync::Arc;
14
15use axum::Router;
16use axum::routing::get;
17
18use crate::session::SessionStore;
19
20/// Shared state passed to all request handlers.
21#[derive(Clone)]
22pub struct AppState {
23    /// Shell binary path (e.g. `/bin/bash`).
24    pub shell: String,
25    /// Working directory for new shell sessions.
26    pub pwd: Option<PathBuf>,
27    /// Scrollback buffer size in bytes.
28    pub scrollback_limit: usize,
29    /// Global session registry.
30    pub sessions: Arc<SessionStore>,
31}
32
33/// Build the Axum router with all routes and shared state.
34pub fn router(
35    shell: String,
36    pwd: Option<PathBuf>,
37    scrollback_limit: usize,
38    sessions: Arc<SessionStore>,
39) -> Router {
40    let state = AppState {
41        shell,
42        pwd,
43        scrollback_limit,
44        sessions,
45    };
46    Router::new()
47        .route("/ws", get(ws::ws_handler))
48        .route("/api/v1/ping", get(health::ping))
49        .route("/", get(static_files::index))
50        .route("/{*path}", get(static_files::static_file))
51        .with_state(state)
52}