Files
Oxicloud/src/interfaces/web/mod.rs
T

75 lines
2.6 KiB
Rust
Raw Normal View History

2025-03-20 09:22:31 +01:00
use crate::common::config::AppConfig;
2026-02-14 01:29:34 +01:00
use crate::common::di::AppState;
use axum::http::header::{CACHE_CONTROL, HeaderValue};
2026-02-14 01:29:34 +01:00
use axum::{Router, response::Html, routing::get};
use std::sync::Arc;
use tower_http::compression::CompressionLayer;
2026-02-14 01:29:34 +01:00
use tower_http::services::ServeDir;
use tower_http::set_header::SetResponseHeaderLayer;
2025-03-17 21:28:08 +01:00
/// Creates web routes for serving static files
pub fn create_web_routes() -> Router<Arc<AppState>> {
2025-03-20 09:22:31 +01:00
// Get config to access static path
let config = AppConfig::from_env();
// In release builds, serve from static-dist/ (processed assets).
// In debug builds, serve from the original static/ directory.
let static_path = if cfg!(not(debug_assertions)) {
let dist = config
.static_path
.parent()
.unwrap_or(std::path::Path::new("."))
.join("static-dist");
if dist.exists() {
dist
} else {
config.static_path.clone()
}
} else {
config.static_path.clone()
};
2025-03-20 09:22:31 +01:00
// Static assets (JS, CSS, JSON, SVG, ICO) served via ServeDir
// with brotli + gzip compression and aggressive caching (7 days).
// HTML pages are served via explicit routes (include_str!) and
// do NOT pass through these layers.
let static_service = ServeDir::new(&static_path);
2025-03-17 21:28:08 +01:00
Router::new()
// Add specific routes for clean URLs (without .html)
2025-03-20 09:22:31 +01:00
.route("/login", get(serve_login_page))
.route("/profile", get(serve_profile_page))
.route("/admin", get(serve_admin_page))
.route("/device", get(serve_device_verify_page))
// Serve static files with compression + cache headers
.fallback_service(static_service)
.layer(CompressionLayer::new().br(true).gzip(true))
.layer(SetResponseHeaderLayer::if_not_present(
CACHE_CONTROL,
HeaderValue::from_static("public, max-age=604800, stale-while-revalidate=86400"),
))
2025-03-20 09:22:31 +01:00
}
/// Serve the login page
async fn serve_login_page() -> Html<&'static str> {
Html(include_str!(concat!(env!("OUT_DIR"), "/login.html")))
}
/// Serve the profile page
async fn serve_profile_page() -> Html<&'static str> {
Html(include_str!(concat!(env!("OUT_DIR"), "/profile.html")))
}
/// Serve the admin page
async fn serve_admin_page() -> Html<&'static str> {
Html(include_str!(concat!(env!("OUT_DIR"), "/admin.html")))
}
/// Serve the device verification page (RFC 8628 Device Authorization Grant)
async fn serve_device_verify_page() -> Html<&'static str> {
Html(include_str!(concat!(
env!("OUT_DIR"),
"/device-verify.html"
)))
}