~aleteoryx/lfm_embed

ref: 83ba4fe37a1184b998be09b9cbe53a22c7ba9e3b lfm_embed/src/config.rs -rw-r--r-- 3.2 KiB
83ba4fe3alyx Move caching to src/cache/; Finalize API parsing fixes 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::collections::BTreeSet;
use std::sync::LazyLock;
use std::sync::Arc;
use std::future::Future;
use std::pin::Pin;
use std::time::*;

use super::cache::AsyncCache;
use super::deserialize::{GetRecentTracks, GetUserInfo, GetTrackInfo, Track, TrackStub, User};

use dotenv::var;
use tokio::sync::RwLock;
use handlebars::{Handlebars, handlebars_helper};
use duration_str as ds;

static INTERNAL_THEMES: &[(&str, &str)] = &[("plain", include_str!("themes/plain.hbs"))];

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

#[derive(Debug)]
pub struct Config {
  pub(crate) lastfm_api_key: Arc<str>,
  port: u16,
  default_theme: Arc<str>,
  send_refresh_header: bool,

  handlebars: Handlebars<'static>,

  pub(crate )google_api_key: Option<Arc<str>>,

  pub(crate) whitelist: BTreeSet<String>,
  pub(crate) whitelist_mode: String,
  pub(crate) default_refresh: Duration,
  pub(crate) whitelist_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);
    Arc::new(Config {
      lastfm_api_key: var("LFME_LASTFM_API_KEY").expect("last.fm API key must be set").into(),
      port: var("LFME_PORT").map(|p| p.parse().expect("cannot parse as a port number")).unwrap_or(9999),
      default_theme: var("LFME_THEME_DEFAULT").map(Into::into).unwrap_or_else(|_| "plain".into()),
      send_refresh_header: !var("LFME_NO_REFRESH").map(|h| &h == "1").unwrap_or(false),

      handlebars: {
        let mut hb = Handlebars::new();
        handlebars_helper!(url_encode: |s: String| urlencoding::encode(&s));

        hb.register_helper("url-encode", Box::new(url_encode));

        for (key, fulltext) in INTERNAL_THEMES {
          log::info!(target: "lfm::config::theme", "Registering internal theme `{key}`");
          hb.register_template_string(key, fulltext).unwrap();
        }
        hb.set_dev_mode(var("LFME_THEME_DEV").map(|h| &h == "1").unwrap_or(false));

        if let Ok(themes_dir) = var("LFME_THEME_DIR") {
          log::info!(target: "lfm::config::theme", "Registering theme dir `{themes_dir}`");
          hb.register_templates_directory(&var("LFME_THEME_EXT").unwrap_or_else(|_| ".hbs".into()), themes_dir).unwrap();
        }

        hb
      },

      google_api_key: var("LFME_GOOGLE_API_KEY").map(Into::into).ok(),

      whitelist: var("LFME_WHITELIST").ok().map(|w| w.split(',').map(|s| s.trim().to_string()).collect()).unwrap_or_default(),
      whitelist_mode: var("LFME_WHITELIST_MODE").map(|m| m.to_ascii_lowercase()).unwrap_or_else(|_| "open".into()),
      default_refresh: default_refresh + Duration::from_secs(1),
      whitelist_refresh: whitelist_refresh + Duration::from_secs(1)
    })
  }

  pub fn port(&self) -> u16 { self.port }
  pub fn send_refresh_header(&self) -> bool { self.send_refresh_header }
  pub fn has_google_api_key(&self) -> bool { self.google_api_key.is_some() }
  pub fn handlebars(&self) -> &Handlebars { &self.handlebars }
  pub fn default_theme(&self) -> Arc<str> { self.default_theme.clone() }
}