Major early updates
This commit is contained in:
37
src/main.rs
37
src/main.rs
@@ -66,8 +66,30 @@ async fn main() -> anyhow::Result<()> {
|
||||
tasks: TaskManager::new(),
|
||||
});
|
||||
|
||||
// Resolve static files directory relative to the binary location
|
||||
let static_dir = std::env::current_exe()
|
||||
.ok()
|
||||
.and_then(|exe| exe.parent().map(|p| p.to_owned()))
|
||||
.map(|p| p.join("static"))
|
||||
.unwrap_or_else(|| std::path::PathBuf::from("static"));
|
||||
|
||||
// Also check next to the crate root (for development)
|
||||
let static_dir = if static_dir.is_dir() {
|
||||
static_dir
|
||||
} else {
|
||||
let dev_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("static");
|
||||
if dev_path.is_dir() {
|
||||
dev_path
|
||||
} else {
|
||||
tracing::warn!("static directory not found — frontend will not be served");
|
||||
static_dir
|
||||
}
|
||||
};
|
||||
tracing::info!(path = %static_dir.display(), "serving static files");
|
||||
|
||||
HttpServer::new(move || {
|
||||
let cors = Cors::permissive();
|
||||
let static_dir = static_dir.clone();
|
||||
|
||||
App::new()
|
||||
.wrap(cors)
|
||||
@@ -75,10 +97,23 @@ async fn main() -> anyhow::Result<()> {
|
||||
.app_data(state.clone())
|
||||
.configure(routes::configure)
|
||||
.service(
|
||||
actix_files::Files::new("/", "static/")
|
||||
actix_files::Files::new("/", static_dir.clone())
|
||||
.index_file("index.html")
|
||||
.prefer_utf8(true),
|
||||
)
|
||||
// SPA fallback: serve index.html for any route not matched
|
||||
// by API or static files, so client-side routing works on refresh
|
||||
.default_service(web::to({
|
||||
let index_path = static_dir.join("index.html");
|
||||
move |req: actix_web::HttpRequest| {
|
||||
let index_path = index_path.clone();
|
||||
async move {
|
||||
actix_files::NamedFile::open_async(index_path)
|
||||
.await
|
||||
.map(|f| f.into_response(&req))
|
||||
}
|
||||
}
|
||||
}))
|
||||
})
|
||||
.bind(&bind)?
|
||||
.run()
|
||||
|
||||
Reference in New Issue
Block a user