aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs248
1 files changed, 124 insertions, 124 deletions
diff --git a/src/config.rs b/src/config.rs
index 8492f8b..49b515e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,4 +1,4 @@
-use std::collections::{HashMap, HashSet};
+use std::collections::BTreeSet;
use std::sync::LazyLock;
use std::sync::Arc;
use std::future::Future;
@@ -17,32 +17,32 @@ use duration_str as ds;
static INTERNAL_THEMES: &[(&'static str, &'static str)] = &[("plain", include_str!("themes/plain.hbs"))];
pub static STATE: LazyLock<Arc<State>> = LazyLock::new(|| {
- State::new()
+ State::new()
});
fn user_getter(username: &String) -> Pin<Box<(dyn Future<Output = Result<Arc<(User, Track)>, (StatusCode, &'static str)>> + Send + Sync)>> {
- let username = urlencoding::encode(username.as_ref()).to_string();
- Box::pin(async move {
- let userreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getInfo&format=json&user={username}&api_key={}", STATE.lastfm_api_key))
- .send().await
- .map_err(|e| {log::error!("Failed to get info for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?;
- if userreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); }
-
- let userinfo = userreq.json::<GetUserInfo>().await
- .map_err(|e| {log::error!("Couldn't parse user.getInfo for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getInfo!")})?.user;
-
- let tracksreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&format=json&extended=1&limit=1&user={username}&api_key={}", STATE.lastfm_api_key))
- .send().await
- .map_err(|e| {log::error!("Failed to get tracks for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?;
- if tracksreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); }
- if tracksreq.status() == StatusCode::FORBIDDEN { return Err((StatusCode::FORBIDDEN, "You need to unprivate your song history!")); }
-
- let tracksinfo = tracksreq.json::<GetRecentTracks>().await
- .map_err(|e| {log::error!("Couldn't parse user.getRecentTracks for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getRecentTracks!")})?
- .recenttracks.track.into_iter().nth(0).ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?;
-
- Ok(Arc::new((userinfo, tracksinfo)))
- })
+ let username = urlencoding::encode(username.as_ref()).to_string();
+ Box::pin(async move {
+ let userreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getInfo&format=json&user={username}&api_key={}", STATE.lastfm_api_key))
+ .send().await
+ .map_err(|e| {log::error!("Failed to get info for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?;
+ if userreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); }
+
+ let userinfo = userreq.json::<GetUserInfo>().await
+ .map_err(|e| {log::error!("Couldn't parse user.getInfo for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getInfo!")})?.user;
+
+ let tracksreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&format=json&extended=1&limit=1&user={username}&api_key={}", STATE.lastfm_api_key))
+ .send().await
+ .map_err(|e| {log::error!("Failed to get tracks for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?;
+ if tracksreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); }
+ if tracksreq.status() == StatusCode::FORBIDDEN { return Err((StatusCode::FORBIDDEN, "You need to unprivate your song history!")); }
+
+ let tracksinfo = tracksreq.json::<GetRecentTracks>().await
+ .map_err(|e| {log::error!("Couldn't parse user.getRecentTracks for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getRecentTracks!")})?
+ .recenttracks.track.into_iter().nth(0).ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?;
+
+ Ok(Arc::new((userinfo, tracksinfo)))
+ })
}
fn font_getter(fontname: &String) -> Pin<Box<(dyn Future<Output = Result<Arc<str>, (StatusCode, &'static str)>> + Send + Sync)>> {
@@ -76,117 +76,117 @@ type FontGetter = fn(&String) -> Pin<Box<(dyn Future<Output = Result<Arc<str>, (
type FontCache = Arc<RwLock<AsyncCache<String, Arc<str>, FontGetter>>>;
#[derive(Debug)]
enum Whitelist {
- Exclusive{cache: UserCache, whitelist: HashSet<String>},
- Open{default_cache: UserCache, whitelist_cache: UserCache, whitelist: HashSet<String>}
+ Exclusive{cache: UserCache, whitelist: BTreeSet<String>},
+ Open{default_cache: UserCache, whitelist_cache: UserCache, whitelist: BTreeSet<String>}
}
#[derive(Debug)]
pub struct State {
- lastfm_api_key: Arc<str>,
- google_api_key: Option<Arc<str>>,
- whitelist: Whitelist,
- port: u16,
+ lastfm_api_key: Arc<str>,
+ port: u16,
+ default_theme: Arc<str>,
+ send_refresh_header: bool,
- handlebars: Handlebars<'static>,
- default_theme: Arc<str>,
- send_refresh_header: bool,
- http: Client,
+ http: Client,
- google_fonts_cache: FontCache,
+ handlebars: Handlebars<'static>,
- default_refresh: Duration,
- whitelist_refresh: Duration,
+ google_api_key: Option<Arc<str>>,
+ google_fonts_cache: FontCache,
+
+ whitelist: Whitelist,
+ default_refresh: Duration,
+ whitelist_refresh: Duration,
}
impl State {
- 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 user_cache_from_duration = |d: Duration| -> UserCache {
- Arc::new(RwLock::new(AsyncCache::new(d, user_getter as UserGetter)))
+ 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 user_cache_from_duration = |d: Duration| -> UserCache {
+ Arc::new(RwLock::new(AsyncCache::new(d, user_getter as UserGetter)))
+ };
+ let default_refresh = duration_from_var("LFME_DEFAULT_REFRESH", 300);
+ let whitelist_refresh = duration_from_var("LFME_WHITELIST_REFRESH", 60);
+ let default_cache = user_cache_from_duration(default_refresh);
+ let whitelist_cache = user_cache_from_duration(whitelist_refresh);
+ Arc::new(State {
+ 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),
+
+ http: Client::builder().https_only(true).user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))).build().unwrap(),
+
+ 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(),
+ google_fonts_cache: Arc::new(RwLock::new(AsyncCache::new(Duration::from_secs(86400), font_getter as FontGetter))),
+
+ whitelist: {
+ let load_whitelist = || -> Option<BTreeSet<String>> {
+ var("LFME_WHITELIST").ok().map(
+ |w| w.split(",").map(|s| s.trim().to_string()).collect()
+ )
};
- let default_refresh = duration_from_var("LFME_DEFAULT_REFRESH", 300);
- let whitelist_refresh = duration_from_var("LFME_WHITELIST_REFRESH", 60);
- let default_cache = user_cache_from_duration(default_refresh);
- let whitelist_cache = user_cache_from_duration(whitelist_refresh);
-
- Arc::new(State {
- 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),
- send_refresh_header: !var("LFME_NO_REFRESH").map(|h| &h == "1").unwrap_or(false),
- http: Client::builder().https_only(true).user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))).build().unwrap(),
-
- 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
- },
- default_theme: var("LFME_THEME_DEFAULT").map(Into::into).unwrap_or_else(|_| "plain".into()),
-
- google_api_key: var("LFME_GOOGLE_API_KEY").map(Into::into).ok(),
- google_fonts_cache: Arc::new(RwLock::new(AsyncCache::new(Duration::from_secs(86400), font_getter as FontGetter))),
-
- whitelist: {
- let load_whitelist = || -> Option<HashSet<String>> {
- 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, 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}`");
- }
- }
- },
- 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 async fn get_fontinfo(&self, font: &String) -> Result<Arc<str>, (StatusCode, &'static str)> {
- self.google_fonts_cache.write().await.get_owned(font).await
- }
- pub fn has_google_api_key(&self) -> bool { self.google_api_key.is_some() }
- pub async fn get_userinfo(&self, user: &String) -> (Result<Arc<(User, Track)>, (StatusCode, &'static str)>, Duration) {
- match &self.whitelist {
- Whitelist::Open{default_cache, whitelist_cache, whitelist} => {
- if whitelist.contains(user) {
- (whitelist_cache.write().await.get_owned(user).await, self.whitelist_refresh)
- }
- else {
- (default_cache.write().await.get_owned(user).await, self.default_refresh)
- }
- },
- Whitelist::Exclusive{cache, whitelist} => {
- if whitelist.contains(user) {
- (cache.write().await.get_owned(user).await, self.whitelist_refresh)
- }
- else { (Err((StatusCode::FORBIDDEN, "User not in whitelist!")), self.default_refresh) }
- }
+ match var("LFME_WHITELIST_MODE").map(|m| m.to_ascii_lowercase()).unwrap_or_else(|_| "open".into()).as_str() {
+ "open" => {
+ Whitelist::Open{default_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}`");
+ }
+ }
+ },
+ 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 async fn get_fontinfo(&self, font: &String) -> Result<Arc<str>, (StatusCode, &'static str)> {
+ self.google_fonts_cache.write().await.get_owned(font).await
+ }
+ pub fn has_google_api_key(&self) -> bool { self.google_api_key.is_some() }
+ pub async fn get_userinfo(&self, user: &String) -> (Result<Arc<(User, Track)>, (StatusCode, &'static str)>, Duration) {
+ match &self.whitelist {
+ Whitelist::Open{default_cache, whitelist_cache, whitelist} => {
+ if whitelist.contains(user) {
+ (whitelist_cache.write().await.get_owned(user).await, self.whitelist_refresh)
+ }
+ else {
+ (default_cache.write().await.get_owned(user).await, self.default_refresh)
+ }
+ },
+ Whitelist::Exclusive{cache, whitelist} => {
+ if whitelist.contains(user) {
+ (cache.write().await.get_owned(user).await, self.whitelist_refresh)
}
+ else { (Err((StatusCode::FORBIDDEN, "User not in whitelist!")), self.default_refresh) }
+ }
}
- pub fn handlebars(&self) -> &Handlebars { &self.handlebars }
- pub fn default_theme(&self) -> Arc<str> { self.default_theme.clone() }
+ }
+ pub fn handlebars(&self) -> &Handlebars { &self.handlebars }
+ pub fn default_theme(&self) -> Arc<str> { self.default_theme.clone() }
}