1.8 KiB · text History 7def6a5
pub mod auth;
pub mod config;
pub mod entities;
pub mod handlers;
pub mod live;
pub mod middleware;
pub mod osm;
pub mod suggest;

pub use config::{create_db_connection, Config};

use minijinja::Environment;
use std::sync::Arc;

/// Human-facing application name. Injected into every template as the
/// `app_name` global (see `build_environment`). Set at generation time from
/// the `app_title` cargo-generate prompt.
pub const APP_NAME: &str = "Fieri, Eternal";

/// Shared, cheaply-clonable state handed to every axum handler.
#[derive(Clone)]
pub struct AppState {
    pub env: Environment<'static>,
    pub db: Arc<sea_orm::DatabaseConnection>,
    pub config: Arc<Config>,
    pub osm: Arc<osm::OsmClient>,
    pub live: live::Live,
}

/// Build the minijinja environment: embedded templates (baked in by build.rs)
/// plus, in debug builds, a path loader so edits under `templates/` show up
/// on the next request without a rebuild. `app_name` is exposed as a global
/// so templates can render the brand without every handler passing it.
pub fn build_environment() -> Environment<'static> {
    let mut env = Environment::new();
    minijinja_embed::load_templates!(&mut env);
    #[cfg(debug_assertions)]
    env.set_loader(minijinja::path_loader("templates"));
    env.add_global("app_name", APP_NAME);
    env.add_global("ranks", ["always", "positive", "neutral", "never"]);
    env
}

/// Globals the map pages need from configuration: the tile server and where
/// to centre when there's no area to centre on.
pub fn add_config_globals(env: &mut Environment<'static>, config: &Config) {
    env.add_global(
        "map_config",
        minijinja::context! {
            tiles => config.tiles_url.trim_end_matches('/'),
            lat => config.default_lat,
            lon => config.default_lon,
        },
    );
}