From 8ea8afd418fcd53889801b4d7048ecea7d036455 Mon Sep 17 00:00:00 2001 From: Artemis Date: Sun, 26 Jan 2025 12:41:07 +0100 Subject: [PATCH] assets_dir fix for deployments --- src/main.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 1a16744..16e8271 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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![