an http based roguelike rpg about jelqing
Blame: src/config.rs
159f0998263b | atagen | 2026-10-12 11:01 UTC | 1 | use std::{path::PathBuf, time::Duration}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 3 | use crate::game::oracle::OracleTemplate; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | use crate::secret::Secret; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | pub struct Config { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | pub addr: String, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | pub secret: Secret, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | pub max_runs: usize, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | pub idle_ttl: Duration, |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 11 | /// Where finished runs are kept. `None` keeps them in memory, which only tests want. |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 12 | pub db: Option<PathBuf>, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 13 | pub(crate) oracle: OracleTemplate, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | impl Default for Config { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | fn default() -> Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | addr: "127.0.0.1:6969".into(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | secret: Secret::random(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | max_runs: 10_000, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | idle_ttl: Duration::from_secs(6 * 60 * 60), |
159f0998263b | atagen | 2026-10-12 11:01 UTC | 23 | db: None, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 24 | oracle: OracleTemplate::Rng, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 29 | /// The XDG state directory, so `nix run` from anywhere finds the same graveyard. |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 30 | fn default_db() -> PathBuf { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 31 | let state = std::env::var_os("XDG_STATE_HOME") |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 32 | .map(PathBuf::from) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 33 | .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".local/state"))); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 34 | match state { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 35 | Some(dir) => dir.join("jelqd").join("jelqd.sqlite"), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 36 | None => PathBuf::from("jelqd.sqlite"), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 37 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 38 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 39 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | impl Config { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | pub fn from_env() -> Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | let var = |name| std::env::var(name).ok(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | let defaults = Self::default(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | Self { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | addr: var("JELQD_ADDR").unwrap_or(defaults.addr), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 46 | db: Some(var("JELQD_DB").map_or_else(default_db, PathBuf::from)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | secret: var("JELQD_SECRET").map_or(defaults.secret, |s| Secret::new(s.into_bytes())), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 48 | oracle: match var("JELQD_ORACLE").as_deref() { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 49 | Some("clm") => OracleTemplate::Clm { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 50 | url: var("JELQD_CLM_URL") |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 51 | .unwrap_or_else(|| "http://127.0.0.1:8700/v1/systemone".into()), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 52 | }, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 53 | _ => defaults.oracle, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 54 | }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | ..defaults |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | } |