Skip to main content

ccs_proxy/api/
ui.rs

1use axum::Router;
2use axum::http::StatusCode;
3use axum::http::header;
4use axum::response::{IntoResponse, Response};
5use axum::routing::get;
6use rust_embed::RustEmbed;
7
8#[derive(RustEmbed)]
9#[folder = "web/"]
10struct WebAsset;
11
12pub fn router() -> Router<crate::AppState> {
13    Router::new()
14        .route("/", get(|| async { serve_asset("index.html") }))
15        .route("/index.html", get(|| async { serve_asset("index.html") }))
16        .route("/app.js", get(|| async { serve_asset("app.js") }))
17        .route("/style.css", get(|| async { serve_asset("style.css") }))
18}
19
20fn serve_asset(name: &str) -> Response {
21    match WebAsset::get(name) {
22        Some(asset) => {
23            let mime = match std::path::Path::new(name)
24                .extension()
25                .and_then(|x| x.to_str())
26            {
27                Some("html") => "text/html; charset=utf-8",
28                Some("js") => "application/javascript; charset=utf-8",
29                Some("css") => "text/css; charset=utf-8",
30                _ => "application/octet-stream",
31            };
32            (
33                StatusCode::OK,
34                [(header::CONTENT_TYPE, mime)],
35                asset.data.into_owned(),
36            )
37                .into_response()
38        }
39        None => (StatusCode::NOT_FOUND, "not found").into_response(),
40    }
41}