aboutsummaryrefslogblamecommitdiffstats
path: root/src/config.rs
blob: 917646ea153c49bd8967721bee43d218d3b472d3 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                         
                               

                        
                 
 
                
                       
 

                                                             

   
                
                   
                                              
                                      
 
            
                            
 

                                         

                                     
                             
 

                                         
                                       
                                         

 
             

                                                                                                                                                                   

                                                                            
                                                                                                                                                           
                     
                                                                                              

                                                                      
                                                                                                        

                                                                                       

                                                                                                  

                                                                                        
                                                                           
 

                                                                                                                                                                 

                       


      
                                                                            

                                                                        
 
// SPDX-License-Identifier: AGPL-3.0-only
use std::collections::BTreeSet;
use std::sync::LazyLock;
use std::sync::Arc;
use std::time::*;

use dotenv::var;
use duration_str as ds;

pub static CONFIG: LazyLock<Arc<Config>> = LazyLock::new(|| {
  Config::new()
});

#[derive(Debug)]
pub struct Config {
  pub(crate) google_api_key: Option<Arc<str>>,
  pub(crate) lastfm_api_key: Arc<str>,

  port: u16,
  send_refresh_header: bool,

  pub(crate) default_theme: Arc<str>,
  pub(crate) theme_dir: Option<Arc<str>>,
  pub(crate) theme_ext_hbs: Arc<str>,
  pub(crate) theme_ext_lua: Arc<str>,
  pub(crate) theme_dev: bool,

  pub(crate) allowlist: BTreeSet<String>,
  pub(crate) allowlist_mode: String,
  pub(crate) default_refresh: Duration,
  pub(crate) allowlist_refresh: Duration,
}

impl Config {
  fn new() -> Arc<Self> {
    let duration_from_var = |v: &str, d: u64| -> Duration {var(v).map(|r| ds::parse(&r).expect("bad duration string")).unwrap_or_else(|_| Duration::from_secs(d))};
    let default_refresh = duration_from_var("LFME_DEFAULT_REFRESH", 300);
    let whitelist_refresh = duration_from_var("LFME_WHITELIST_REFRESH", 60);
    let allowlist_refresh = if whitelist_refresh == Duration::from_secs(60) { duration_from_var("LFME_ALLOWLIST_REFRESH", 60) } else { whitelist_refresh };
    Arc::new(Config {
      lastfm_api_key: var("LFME_LASTFM_API_KEY").expect("last.fm API key must be set").into(),
      google_api_key: var("LFME_GOOGLE_API_KEY").map(Into::into).ok(),

      port: var("LFME_PORT").map(|p| p.parse().expect("cannot parse as a port number")).unwrap_or(9999),
      send_refresh_header: !var("LFME_NO_REFRESH").map(|h| &h == "1").unwrap_or(false),

      default_theme: var("LFME_THEME_DEFAULT").map(Into::into).unwrap_or_else(|_| "plain".into()),
      theme_dir: var("LFME_THEME_DIR").ok().map(Into::into),
      theme_ext_hbs: var("LFME_THEME_EXT_HBS").unwrap_or_else(|_| ".hbs".into()).into(),
      theme_ext_lua: var("LFME_THEME_EXT_LUA").unwrap_or_else(|_| ".lua".into()).into(),
      theme_dev: var("LFME_THEME_DEV").map(|h| &h == "1").unwrap_or(false),

      allowlist: var("LFME_WHITELIST").or_else(|_| var("LFME_ALLOWLIST")).ok().map(|w| w.split(',').map(|s| s.trim().to_string()).collect()).unwrap_or_default(),
      allowlist_mode: var("LFME_WHITELIST_MODE").or_else(|_| var("LFME_ALLOWLIST_MODE")).map(|m| m.to_ascii_lowercase()).unwrap_or_else(|_| "open".into()),
      default_refresh,
      allowlist_refresh
    })
  }

  pub fn has_google_api_key(&self) -> bool { self.google_api_key.is_some() }
  pub fn port(&self) -> u16 { self.port }
  pub fn send_refresh_header(&self) -> bool { self.send_refresh_header }
}