~aleteoryx/lfm_embed

83ba4fe37a1184b998be09b9cbe53a22c7ba9e3b — alyx 8 months ago 22c2e4e
Move caching to src/cache/; Finalize API parsing fixes

Font and user cache code has been moved to special files, independant from src/config.rs

API parsing changes have been properly tested, and last.fm API JSON is now trace-logged for debugging convenience.
9 files changed, 168 insertions(+), 145 deletions(-)

M src/cache.rs
R src/{font.rs => cache/font.rs}
A src/cache/user.rs
M src/config.rs
M src/ctx.rs
M src/deserialize.rs
A src/http.rs
M src/lib.rs
M src/main.rs
M src/cache.rs => src/cache.rs +6 -3
@@ 1,3 1,6 @@
pub mod font;
pub mod user;

use std::future::Future;
use std::time::*;
use std::collections::HashMap;


@@ 31,10 34,10 @@ where

  pub async fn get(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> {
    if self.is_stale(key) {
      log::trace!(target: "lfm::cache", "MISS : interval = {:?}", self.interval);
      log::trace!("MISS : interval = {:?}", self.interval);
      self.renew(key).await
    } else {
      log::trace!(target: "lfm::cache", "HIT : interval = {:?}", self.interval);
      log::trace!("HIT : interval = {:?}", self.interval);
      Ok(&self.cache.get(key).unwrap().1)
    }
  }


@@ 48,7 51,7 @@ where
  pub fn is_stale(&self, key: &K) -> bool {
    if let Some((last_update, _)) = self.cache.get(key) {
      let now = Instant::now();
      log::trace!(target: "lfm::cache", "Key exists, last update {:?} ago.", now - *last_update);
      log::trace!("Key exists, last update {:?} ago.", now - *last_update);
      now > (*last_update + self.interval)
    }
    else { true }

R src/font.rs => src/cache/font.rs +14 -7
@@ 1,11 1,12 @@
use std::sync::Arc;
use std::time::Duration;
use std::sync::LazyLock;

use tokio::sync::RwLock;
use reqwest::StatusCode;
use reqwest::{StatusCode, Client};

use super::cache::{CacheFuture, CacheGetter, Cache, AsyncCache};
use crate::STATE;
use super::{CacheFuture, CacheGetter, Cache, AsyncCache};
use crate::CONFIG;

#[derive(serde::Deserialize, Debug, Default)]
#[serde(default)]


@@ 24,12 25,12 @@ pub type FontCache  = Cache<Arc<str>>;
fn font_getter(fontname: &String) -> FontFuture {
  let fontname = urlencoding::encode(fontname.as_ref()).to_string();
  Box::pin(async move {
    let Some(google_api_key) = STATE.google_api_key.clone()
    let Some(google_api_key) = CONFIG.google_api_key.clone()
    else {
      unreachable!();
    };

    let fontreq = STATE.http.get(format!("https://www.googleapis.com/webfonts/v1/webfonts?key={}&family={fontname}", google_api_key))
    let fontreq = HTTP.get(format!("https://www.googleapis.com/webfonts/v1/webfonts?key={}&family={fontname}", google_api_key))
      .send().await
      .map_err(|e| {log::error!("Failed to get info for font `{fontname}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to Google Fonts!")})?;
    if fontreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "Font does not exist!")); }


@@ 38,7 39,7 @@ fn font_getter(fontname: &String) -> FontFuture {
      return Err((StatusCode::SERVICE_UNAVAILABLE, "This instance is not configured to support Google Fonts properly, please use a different font."));
    }

    let cssreq = STATE.http.get(format!("https://fonts.googleapis.com/css2?family={fontname}"))
    let cssreq = HTTP.get(format!("https://fonts.googleapis.com/css2?family={fontname}"))
      .send().await
      .map_err(|e| {log::error!("Failed to get CSS for font `{fontname}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't download font CSS!")})?;



@@ 46,6 47,12 @@ fn font_getter(fontname: &String) -> FontFuture {
  })
}

pub fn font_cache() -> FontCache {
static HTTP: LazyLock<Client> = crate::http::lazy();

static FONT_CACHE: LazyLock<FontCache> =  LazyLock::new(|| {
  Arc::new(RwLock::new(AsyncCache::new(Duration::from_secs(86400), font_getter as FontGetter)))
});

pub async fn get_fontinfo(font: &String) -> Result<Arc<str>, (StatusCode, &'static str)> {
  FONT_CACHE.write().await.get_owned(font).await
}

A src/cache/user.rs => src/cache/user.rs +101 -0
@@ 0,0 1,101 @@
use std::sync::Arc;
use std::time::Duration;
use std::sync::LazyLock;

use tokio::sync::RwLock;
use reqwest::{StatusCode, Client};

use super::{CacheFuture, CacheGetter, Cache, AsyncCache};
use crate::deserialize::{User, Track, TrackStub};
use crate::CONFIG;

type UserFuture = CacheFuture<Arc<(User, Track, TrackStub)>>;
type UserGetter = CacheGetter<Arc<(User, Track, TrackStub)>>;
type UserCache  = Cache<Arc<(User, Track, TrackStub)>>;

#[derive(Debug)]
enum Whitelist {
  Exclusive{cache: UserCache},
  Open{default_cache: UserCache, whitelist_cache: UserCache}
}

fn user_getter(username: &String) -> UserFuture {
  use crate::deserialize::{GetUserInfo, GetRecentTracks, GetTrackInfo};

  let username = urlencoding::encode(username.as_ref()).to_string();
  Box::pin(async move {
    let userreq = HTTP.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getInfo&format=json&user={username}&api_key={}", CONFIG.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 userstr = userreq.text().await.unwrap();
    log::trace!("Got user.getUserInfo JSON for `{username}`: {userstr}");
    let userinfo = serde_json::from_str::<GetUserInfo>(&userstr)
      .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 = HTTP.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&format=json&extended=1&limit=1&user={username}&api_key={}", CONFIG.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 tracksstr = tracksreq.text().await.unwrap();
    log::trace!("Got user.getRecentTracks JSON for `{username}`: {tracksstr}");
    let trackstub = serde_json::from_str::<GetRecentTracks>(&tracksstr)
      .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().next().ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?;

    let trackreq = HTTP.get(format!("https://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json&username={username}&api_key={}&track={}&artist={}", CONFIG.lastfm_api_key, trackstub.name, trackstub.artist.name))
      .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 trackreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "Track does not exist!")); }

    let trackstr = trackreq.text().await.unwrap();
    log::trace!("Got user.getTrackInfo JSON for `{username}`: {trackstr}");
    let trackinfo = serde_json::from_str::<GetTrackInfo>(&trackstr)
      .map_err(|e| {log::error!("Couldn't parse track.getInfo for `{}` by `{}` on behalf of {username}: {e}", trackstub.name, trackstub.artist.name); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse track.getInfo!")})?.track;

    Ok(Arc::new((userinfo, trackinfo, trackstub)))
  })
}

static HTTP: LazyLock<Client> = crate::http::lazy();

static WHITELIST: LazyLock<Whitelist> = LazyLock::new(|| {
  let default_cache = Arc::new(RwLock::new(AsyncCache::new(CONFIG.default_refresh, user_getter as UserGetter)));
  let whitelist_cache = Arc::new(RwLock::new(AsyncCache::new(CONFIG.whitelist_refresh, user_getter as UserGetter)));
  match CONFIG.whitelist_mode.as_str() {
    "open" => {
      Whitelist::Open{default_cache, whitelist_cache}
    },
    "exclusive" => {
      if CONFIG.whitelist.is_empty() {
        panic!("Exclusive mode set with empty whitelist, cannot serve any requests!");
      }
      Whitelist::Exclusive{cache: whitelist_cache}
    },
    m => {
      panic!("Bad whitelist mode: `{m}`");
    }
  }
});

pub async fn get_userinfo(user: &String) -> (Result<Arc<(User, Track, TrackStub)>, (StatusCode, &'static str)>, Duration) {
  match LazyLock::force(&WHITELIST) {
    Whitelist::Open{default_cache, whitelist_cache} => {
      if CONFIG.whitelist.contains(user) {
        (whitelist_cache.write().await.get_owned(user).await, CONFIG.whitelist_refresh)
      }
      else {
        (default_cache.write().await.get_owned(user).await, CONFIG.default_refresh)
      }
    },
    Whitelist::Exclusive{cache} => {
      if CONFIG.whitelist.contains(user) {
        (cache.write().await.get_owned(user).await, CONFIG.whitelist_refresh)
      }
      else { (Err((StatusCode::FORBIDDEN, "User not in whitelist!")), CONFIG.default_refresh) }
    }
  }
}

M src/config.rs => src/config.rs +14 -110
@@ 7,103 7,46 @@ use std::time::*;

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

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

type CacheFuture<Output> = Pin<Box<(dyn Future<Output = Result<Output, (StatusCode, &'static str)>> + Send + Sync)>>;
type CacheGetter<Output> = fn(&String) -> CacheFuture<Output>;
type Cache<Output>       = Arc<RwLock<AsyncCache<String, Output, CacheGetter<Output>>>>;

type UserFuture = CacheFuture<Arc<(User, Track, TrackStub)>>;
type UserGetter = CacheGetter<Arc<(User, Track, TrackStub)>>;
type UserCache  = Cache<Arc<(User, Track, TrackStub)>>;

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

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

fn user_getter(username: &String) -> UserFuture {
  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 trackstub = 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().next().ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?;

    let trackreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json&username={username}&api_key={}&track={}&artist={}", STATE.lastfm_api_key, trackstub.name, trackstub.artist.name))
      .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 trackreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "Track does not exist!")); }

    let trackinfo = trackreq.json::<GetTrackInfo>().await
      .map_err(|e| {log::error!("Couldn't parse track.getInfo for `{}` by `{}` on behalf of {username}: {e}", trackstub.name, trackstub.artist.name); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse track.getInfo!")})?.track;

    Ok(Arc::new((userinfo, trackinfo, trackstub)))
  })
}

#[derive(Debug)]
enum Whitelist {
  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>,
pub struct Config {
  pub(crate) lastfm_api_key: Arc<str>,
  port: u16,
  default_theme: Arc<str>,
  send_refresh_header: bool,

  pub(crate) http: Client,

  handlebars: Handlebars<'static>,

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

  whitelist: Whitelist,
  default_refresh: Duration,
  whitelist_refresh: Duration,
  pub(crate) whitelist: BTreeSet<String>,
  pub(crate) whitelist_mode: String,
  pub(crate) default_refresh: Duration,
  pub(crate) whitelist_refresh: Duration,
}

impl State {
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 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 {
    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),

      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));


@@ 125,27 68,9 @@ impl State {
      },

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

      whitelist: {
        let load_whitelist = || -> Option<BTreeSet<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}`");
          }
        }
      },

      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)
    })


@@ 153,28 78,7 @@ impl State {

  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, TrackStub)>, (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() }
}

M src/ctx.rs => src/ctx.rs +5 -5
@@ 2,8 2,8 @@ use reqwest::StatusCode;
use super::deserialize as de;
use std::sync::Arc;
use std::collections::BTreeMap;
use super::font::FontQuery;
use super::config::STATE;
use crate::cache::font::FontQuery;
use super::config::CONFIG;

pub mod model {
  use std::sync::Arc;


@@ 126,7 126,7 @@ impl ResponseCtx {
          },
          scrobble: model::Scrobble {
            name: track.name.clone(),
            album: track.album.name.clone(),
            album: track.album.title.clone(),
            artist: model::Artist {
              name: track.artist.name.clone(),
//              image_url: track.artist.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()),


@@ 138,8 138,8 @@ impl ResponseCtx {
            loved: track.loved.unwrap_or(false)
          },
          font: match font_query {
            Some(FontQuery { google_font: Some(f), .. }) if STATE.has_google_api_key() => {
              let css = match STATE.get_fontinfo(&f.to_string()).await {
            Some(FontQuery { google_font: Some(f), .. }) if CONFIG.has_google_api_key() => {
              let css = match crate::cache::font::get_fontinfo(&f.to_string()).await {
                Ok(css) => css,
                Err((status, error)) => { return ResponseCtx(model::Root::Error {error}, status); }
              };

M src/deserialize.rs => src/deserialize.rs +8 -8
@@ 82,16 82,15 @@ pub enum ImageSize {
#[derive(Deserialize, Debug)]
pub struct Image {
  pub size: ImageSize,
  #[serde(rename = "#text")]
  #[serde(alias = "#text")]
  pub url: Arc<str>,
}

#[derive(Deserialize, Debug)]
pub struct Album {
  #[serde(rename = "mbid")]
  pub uuid: Arc<str>,
  #[serde(rename = "#text")]
  pub name: Arc<str>,
  pub mbid: Option<Arc<str>>,
  #[serde(alias = "#text")]
  pub title: Arc<str>,
  #[serde(default)]
  #[serde(rename = "image")]
  pub images: Vec<Image>,


@@ 102,9 101,10 @@ pub struct Album {
#[derive(Deserialize, Debug)]
pub struct Track {
  pub artist: Artist,
  #[serde(rename = "image")]
  #[serde(alias = "image")]
  #[serde(default)]
  pub images: Vec<Image>,
  pub mbid: Arc<str>,
  pub mbid: Option<Arc<str>>,
  pub album: Album,
  pub name: Arc<str>,
  pub url: Arc<str>,


@@ 138,7 138,7 @@ pub struct TrackAttr {
}
#[derive(Deserialize, Debug)]
pub struct ArtistStub {
  #[serde(rename = "#text")]
  #[serde(alias = "#text")]
  pub name: Arc<str>
}
#[derive(Deserialize, Debug)]

A src/http.rs => src/http.rs +9 -0
@@ 0,0 1,9 @@
use std::sync::LazyLock;
use reqwest::Client;

pub(crate) fn new() -> Client {
  Client::builder().https_only(true).user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))).build().unwrap()
}
pub(crate) const fn lazy() -> LazyLock<Client> {
  LazyLock::new(new)
}

M src/lib.rs => src/lib.rs +3 -2
@@ 1,10 1,11 @@
#![feature(lazy_cell)]

mod http;

pub mod deserialize;
pub mod cache;
pub mod config;
pub mod ctx;
pub mod font;

pub use config::STATE;
pub use config::CONFIG;
pub use ctx::ResponseCtx;

M src/main.rs => src/main.rs +8 -10
@@ 5,8 5,8 @@ use log::LevelFilter;
use std::collections::BTreeMap;
use std::fs::File;
use std::sync::Arc;
use lfm_embed::{STATE, ResponseCtx};
use lfm_embed::font::FontQuery;
use lfm_embed::{CONFIG, ResponseCtx};
use lfm_embed::cache::font::FontQuery;
use warp::Filter;

#[derive(serde::Deserialize, Debug)]


@@ 38,22 38,20 @@ async fn main() {
        .unwrap_or(env_logger::Target::Stderr)
    ).init();

  std::sync::LazyLock::force(&STATE);

  let user = warp::path!("user" / String)
    .and(warp::query::<UserQuery>())
    .then(|s, q: UserQuery| async move {
      log::debug!(target: "lfm::server::user", "Handling request for user `{s}` with {q:?}");
      let (ctx, dur) = STATE.get_userinfo(&s).await;
      log::debug!(target: "lfm_embed::server::user", "Handling request for user `{s}` with {q:?}");
      let (ctx, dur) = lfm_embed::cache::user::get_userinfo(&s).await;
      let ResponseCtx(data, status) = ResponseCtx::create(ctx, q.font, q.rest).await;

      let theme = q.theme.filter(|a| STATE.handlebars().has_template(a)).unwrap_or_else(|| STATE.default_theme());
      log::debug!(target: "lfm::server::user", "Using theme {theme}");
      let theme = q.theme.filter(|a| CONFIG.handlebars().has_template(a)).unwrap_or_else(|| CONFIG.default_theme());
      log::debug!(target: "lfm_embed::server::user", "Using theme {theme}");
      warp::reply::with_header(
        warp::reply::with_header(
          warp::reply::with_status(
            warp::reply::html(
              STATE.handlebars().render(&theme, &data).unwrap()
              CONFIG.handlebars().render(&theme, &data).unwrap()
            ), status
          ), "Refresh", dur.as_secs()
        ), "X-Selected-Theme", theme.as_ref()


@@ 61,5 59,5 @@ async fn main() {
    });

  warp::serve(user)
    .bind(([127,0,0,1], STATE.port())).await;
    .bind(([127,0,0,1], CONFIG.port())).await;
}