Implement support for serving webui

This commit is contained in:
Mathias Wagner
2025-09-09 09:53:30 +02:00
parent 1936304e56
commit efe4549f82
3 changed files with 64 additions and 10 deletions

View File

@@ -4,18 +4,15 @@ mod utils;
use utils::init_database;
use anyhow::Result;
use axum::{
routing::{delete, get, post, put},
Router,
};
use axum::{routing::{delete, get, post, put}, Router};
use routes::{admin, auth as auth_routes, machines, setup};
use tower_http::cors::CorsLayer;
use tower_http::{cors::CorsLayer, services::{ServeDir, ServeFile}};
use std::path::Path;
#[tokio::main]
async fn main() -> Result<()> {
let pool = init_database().await?;
let app = Router::new()
let api_routes = Router::new()
.route("/setup/status", get(setup::get_setup_status))
.route("/setup/init", post(setup::init_setup))
@@ -35,10 +32,20 @@ async fn main() -> Result<()> {
.with_state(pool);
let dist_path = "../dist";
let app = Router::new()
.nest("/api", api_routes)
.nest_service("/assets", ServeDir::new(format!("{}/assets", dist_path)))
.route_service("/", ServeFile::new(format!("{}/index.html", dist_path)))
.fallback_service(ServeFile::new(format!("{}/index.html", dist_path)))
.layer(CorsLayer::permissive());
if !Path::new(dist_path).exists() {
println!("Warning: dist directory not found at {}", dist_path);
}
let listener = tokio::net::TcpListener::bind("0.0.0.0:8379").await?;
println!("Server running on http://0.0.0.0:8379");
axum::serve(listener, app).await?;
Ok(())
}