Skip to content
1        10        20        30        40        50        60        70        801//! Runtime configuration.2//!3//! Every knob is an environment variable so the same binary works from `mise`4//! during development and from the container image in production. The model5//! weights are never baked into the image: they are downloaded on startup into6//! `MODEL_DIR` (default `<STORAGE_DIR>/<MODEL_REPO>`) when missing.78use std::env;9use std::path::{Path, PathBuf};1011const DEFAULT_MODEL_REPO: &str = "fastino/GLiNER2.5-Decide";12const DEFAULT_STORAGE_DIR: &str = "/storage";13const DEFAULT_HF_ENDPOINT: &str = "https://huggingface.co";14const DEFAULT_PORT: u16 = 8080;1516#[derive(Debug, Clone)]17pub struct Config {18    /// TCP port for the HTTP server.19    pub port: u16,20    /// Root directory that holds downloaded model directories.21    pub storage_dir: PathBuf,22    /// Hub repository the weights are fetched from.23    pub model_repo: String,24    /// Directory the checkpoint is read from, and downloaded into when incomplete.25    pub model_dir: PathBuf,26    /// Hub endpoint, overridable for mirrors.27    pub hf_endpoint: String,28    /// Key the `/v1` routes require from `API_KEY`; the `/` playground embeds it.29    pub api_key: Option<String>,30    /// Optional Hub token for gated repositories.31    pub hf_token: Option<String>,32}3334impl Config {35    /// Read the configuration from the environment.36    ///37    /// # Errors38    /// Returns a message when `PORT` is not a TCP port.39    pub fn from_env() -> Result<Self, String> {40        let port = match env::var("PORT") {41            Ok(value) => value42                .parse::<u16>()43                .map_err(|error| format!("PORT must be a TCP port: {error}"))?,44            Err(_) => DEFAULT_PORT,45        };4647        let model_repo = var_or("MODEL_REPO", DEFAULT_MODEL_REPO);48        let storage_dir = PathBuf::from(var_or("STORAGE_DIR", DEFAULT_STORAGE_DIR));49        let model_dir = match env::var("MODEL_DIR") {50            Ok(value) => PathBuf::from(value),51            Err(_) => storage_dir.join(&model_repo),52        };5354        Ok(Self {55            port,56            storage_dir,57            model_repo,58            model_dir,59            hf_endpoint: var_or("HF_ENDPOINT", DEFAULT_HF_ENDPOINT)60                .trim_end_matches('/')61                .to_owned(),62            api_key: env::var("API_KEY")63                .ok()64                .map(|value| value.trim().to_owned())65                .filter(|key| !key.is_empty()),66            hf_token: env::var("HF_TOKEN").ok().filter(|token| !token.is_empty()),67        })68    }6970    /// Human readable label used in `/health` and log lines.71    #[must_use]72    pub fn model_label(&self) -> String {73        format!("{} at {}", self.model_repo, display(&self.model_dir))74    }75}7677fn var_or(name: &str, fallback: &str) -> String {78    env::var(name)79        .ok()80        .filter(|value| !value.is_empty())81        .unwrap_or_else(|| fallback.to_owned())82}8384fn display(path: &Path) -> String {85    path.canonicalize().map_or_else(86        |_| path.display().to_string(),87        |resolved| resolved.display().to_string(),88    )89}