aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 6b1aac0bee704fd2e8a9b09c5b9a7190d795f851 (plain) (blame)
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
// SPDX-License-Identifier: AGPL-3.0-only
#![feature(lazy_cell)]

use std::collections::BTreeMap;
use std::fs::File;
use std::sync::Arc;

use lfm_embed::CONFIG;
use lfm_embed::ctx::get_ctx;
use lfm_embed::cache::user::get_userinfo;
use lfm_embed::cache::font::FontQuery;
use lfm_embed::theming::render_theme;

use log::LevelFilter;
use dotenv::var;
use warp::Filter;


#[derive(serde::Deserialize, Debug)]
#[serde(rename = "kebab-case")]
struct UserQuery {
  #[serde(default)]
  theme: Option<Arc<str>>,
  #[serde(flatten)]
  #[serde(default)]
  font: Option<FontQuery>,
  #[serde(flatten)]
  rest: BTreeMap<String, String>
}

#[tokio::main]
async fn main() {
  env_logger::Builder::new()
    .filter_level(LevelFilter::Warn)
    .parse_filters(&var("LFME_LOG_LEVEL").unwrap_or_default())
    .target(
      var("LFME_LOG_FILE").ok()
        .map(
          |f| env_logger::Target::Pipe(
            Box::new(File::options()
              .append(true)
              .open(f)
              .expect("couldn't open LFME_LOG_FILE")))
        )
        .unwrap_or(env_logger::Target::Stderr)
    ).init();

  lfm_embed::touch();

  let user = warp::path!("user" / String)
    .and(warp::query::<UserQuery>())
    .then(|s, q: UserQuery| async move {
      log::debug!(target: "lfm_embed::main::user", "Handling request for user `{s}` with {q:?}");
      let (userinfo, refresh) = get_userinfo(&s).await;
      let (ctx, status) = get_ctx(userinfo, q.font, q.rest).await;

      let (theme, res) = tokio::task::spawn_blocking(move || render_theme(q.theme.as_deref(), &ctx)).await.expect("fatal error in theme rendering");
      log::debug!(target: "lfm_embed::main::user", "Using theme {theme}");

      match res {
        Err(status) =>
          http::Response::builder()
            .status(status)
            .header("Content-Type", "text/html")
            .body("<h1>Internal Server Error.</h1><p>Occurred when templating, check the logs.</p>".into())
            .unwrap(),

        Ok(contents) =>
          http::Response::builder()
            .status(status)
            .header("Refresh", refresh.as_secs())
            .header("X-Selected-Theme", theme)
            .header("Content-Type", "text/html")
            .body(contents)
            .unwrap()
      }
    });

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