assets_dir fix for deployments

This commit is contained in:
Artemis 2025-01-26 12:41:07 +01:00
parent c7cb589442
commit 8ea8afd418

View file

@ -1,6 +1,9 @@
#[macro_use]
extern crate rocket;
use std::env;
use std::path::PathBuf;
use auth::session;
use db::migrate::run_migrations;
use db::schema::DollTags;
@ -18,6 +21,29 @@ pub mod routes;
#[launch]
fn rocket() -> _ {
// in dev., this is easy to get right, but in prod in special systems such as nixos, it's annoying to predict.
// instead, we can simply set it at runtime.
// Priority order:
// 1. environment variable
// 2. `[..]/bin/dolltags/../lib/assets` (folder containing `/bin/dolltags` and `/lib/assets`)
// 3. `/assets` relative to cargo manifest (dev env)
let assets_path = env::var("ASSETS_PATH")
.map(|v| PathBuf::from(v))
.ok()
.or_else(|| {
env::current_exe().ok().and_then(|path| {
// path = /bin/dolltags
// path.parent.parent = /
let assets_dir = path.parent()?.parent()?.join("lib/assets");
if assets_dir.exists() {
Some(assets_dir)
} else {
None
}
})
})
.unwrap_or_else(|| PathBuf::from(relative!("/assets")));
rocket::build()
.attach(pages::init_templates())
.attach(DollTags::init())
@ -26,7 +52,7 @@ fn rocket() -> _ {
"/",
catchers![error_handlers::not_found, session::unauthorized],
)
.mount("/assets", FileServer::from(relative!("/assets")))
.mount("/assets", FileServer::from(assets_path))
.mount(
"/account",
routes![