LINE1 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};1011constDEFAULT_MODEL_REPO: &str = "fastino/GLiNER2.5-Decide";12constDEFAULT_STORAGE_DIR: &str = "/storage";13constDEFAULT_HF_ENDPOINT: &str = "https://huggingface.co";14constDEFAULT_PORT: u16 = 8080;1516#[derive(Debug, Clone)]17pubstructConfig {18/// TCP port for the HTTP server.19pub port: u16,20/// Root directory that holds downloaded model directories.21pub storage_dir: PathBuf,22/// Hub repository the weights are fetched from.23pub model_repo: String,24/// Directory the checkpoint is read from, and downloaded into when incomplete.25pub model_dir: PathBuf,26/// Hub endpoint, overridable for mirrors.27pub hf_endpoint: String,28/// Key the `/v1` routes require from `API_KEY`; the `/` playground embeds it.29pub api_key: Option<String>,30/// Optional Hub token for gated repositories.31pub hf_token: Option<String>,32}3334implConfig {35/// Read the configuration from the environment.36///37/// # Errors38/// Returns a message when `PORT` is not a TCP port.39pubfnfrom_env() -> Result<Self, String> {40let port = match env::var("PORT") {41Ok(value) => value42 .parse::<u16>()43 .map_err(|error| format!("PORT must be a TCP port: {error}"))?,44Err(_) => DEFAULT_PORT,45 };4647let model_repo = var_or("MODEL_REPO", DEFAULT_MODEL_REPO);48let storage_dir = PathBuf::from(var_or("STORAGE_DIR", DEFAULT_STORAGE_DIR));49let model_dir = match env::var("MODEL_DIR") {50Ok(value) => PathBuf::from(value),51Err(_) => storage_dir.join(&model_repo),52 };5354Ok(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]72pubfnmodel_label(&self) -> String {73format!("{} at {}", self.model_repo, display(&self.model_dir))74 }75}7677fnvar_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}8384fndisplay(path: &Path) -> String {85 path.canonicalize().map_or_else(86 |_| path.display().to_string(),87 |resolved| resolved.display().to_string(),88 )89}