From d0fc16dde4cf1c58769dfff058509677f601eb8e Mon Sep 17 00:00:00 2001 From: alyx Date: Wed, 9 Aug 2023 15:04:41 -0400 Subject: config done --- src/config.rs | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/config.rs (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..ef9c709 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,96 @@ +use std::collections::{HashMap, HashSet}; +use std::error::Error; +use std::sync::LazyLock; +use std::sync::Arc; +use std::future::Future; +use std::pin::Pin; +use std::fs; +use std::time::*; +use duration_str as ds; + +use super::cache::AsyncCache; +use super::deserialize::{GetRecentTracks, GetUserInfo, Track, User}; + +use reqwest::Client; +use dotenv::var; +use tokio::sync::RwLock; + +pub static STATE: LazyLock> = LazyLock::new(|| { + State::new() +}); + +fn getter(username: &String) -> Pin>>> { + Box::pin(async{Err("nope")}) +} + +type Cache = Arc Pin>>>>>>; +#[derive(Debug)] +enum Whitelist { + Exclusive{cache: Cache, whitelist: HashSet}, + Open{default_cache: Cache, whitelist_cache: Cache, whitelist: HashSet} +} +#[derive(Debug)] +pub struct State { + api_key: Arc, + whitelist: Whitelist, + port: u16, + custom_themes: HashMap>, + send_refresh_header: bool +} + +impl State { + fn new() -> Arc { + Arc::new(State { + api_key: var("LFME_API_KEY").expect("API key must be set").into(), + port: var("LFME_PORT").map(|p| p.parse().expect("cannot parse as a port number")).unwrap_or(9999), + send_refresh_header: var("LFME_SET_HEADER").map(|h| &h == "1").unwrap_or(false), + + custom_themes: { + if let Ok(themes_dir) = var("LFME_THEMES_DIR") { + fs::read_dir(themes_dir).expect("error reading LFME_THEMES_DIR") + .map(|a| a.expect("error reading LFME_THEMES_DIR")) + .filter_map(|a| { + let path = a.path(); + if fs::metadata(&path).map(|m| m.is_file()).unwrap_or(false) && + path.extension() == Some("css".as_ref()) { + Some((path.file_stem().unwrap().to_str().expect("bad filename").to_string(), fs::read_to_string(&path).expect("couldn't read theme CSS").into())) + } + else { None } + }) + .collect() + } + else { HashMap::new() } + }, + + whitelist: { + let cache_from_var = |v: &str, d: u64| -> Cache { + let refresh = var(v).map(|r| ds::parse(&r).expect("bad duration string")).unwrap_or_else(|_| Duration::from_secs(d)); + Arc::new(RwLock::new(AsyncCache::new(refresh, getter as _))) + }; + let default_cache = || cache_from_var("LFME_DEFAULT_REFRESH", 300); + let whitelist_cache = || cache_from_var("LFME_WHITELIST_REFRESH", 60); + + let load_whitelist = || -> Option> { + var("LFME_WHITELIST").ok().map( + |w| w.split(",").map(|s| s.trim().to_string()).collect() + ) + }; + + match var("LFME_WHITELIST_MODE").map(|m| m.to_ascii_lowercase()).unwrap_or_else(|_| "open".into()).as_str() { + "open" => { + Whitelist::Open{default_cache: default_cache(), whitelist_cache: whitelist_cache(), whitelist: load_whitelist().unwrap_or_default()} + }, + "exclusive" => { + Whitelist::Exclusive{cache: whitelist_cache(), whitelist: load_whitelist().expect("LFME_WHITELIST not set, unable to serve anyone")} + }, + m => { + panic!("Bad whitelist mode: `{m}`"); + } + } + } + }) + } + + pub fn api_key(&self) -> Arc { self.api_key.clone() } + pub fn port(&self) -> u16 { self.port } +} -- cgit v1.2.3-54-g00ecf