aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 11da61c0851d995957afb3fc08f6d21357693b4c (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// This file is part of Locus <https://github.com/gemrest/locus>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

#![feature(once_cell, is_some_with)]
#![deny(
  warnings,
  nonstandard_style,
  unused,
  future_incompatible,
  rust_2018_idioms,
  unsafe_code
)]
#![deny(clippy::all, clippy::nursery, clippy::pedantic)]
#![recursion_limit = "128"]
#![allow(clippy::cast_precision_loss)]

mod macros;
mod modules;
mod route;
mod search;

#[macro_use]
extern crate log;

use std::{collections::HashMap, lazy::SyncLazy, sync::Mutex};

use pickledb::PickleDb;
use tokio::time::Instant;
use yarte::Template;

const SEARCH_SIZE: usize = 10;
const ERROR_HANDLER_RESPONSE: &str =
  "The requested resource could not be found at this time. You can try \
   refreshing the page, if that doesn't change anything; contact Fuwn! \
   ([email protected])";

static DATABASE: SyncLazy<Mutex<PickleDb>> = SyncLazy::new(|| {
  Mutex::new({
    if std::fs::File::open(".locus/locus.db").is_ok() {
      PickleDb::load_json(
        ".locus/locus.db",
        pickledb::PickleDbDumpPolicy::AutoDump,
      )
      .unwrap()
    } else {
      PickleDb::new_json(
        ".locus/locus.db",
        pickledb::PickleDbDumpPolicy::AutoDump,
      )
    }
  })
});
static ROUTES: SyncLazy<Mutex<HashMap<String, route::Route>>> =
  SyncLazy::new(|| Mutex::new(HashMap::new()));

#[derive(Template)]
#[template(path = "main")]
struct Main<'a> {
  body:        &'a str,
  hits:        &'a i32,
  quote:       &'a str,
  commit:      &'a str,
  mini_commit: &'a str,
}

fn time_mounts<T>(context: &str, timer: &mut Instant, mut mounter: T)
where T: FnMut() {
  mounter();

  info!(
    "{} mounts took {}ms",
    context,
    timer.elapsed().as_nanos() as f64 / 1_000_000.0
  );

  *timer = Instant::now();
}

fn time_section(timer: &mut Instant, context: &str) {
  info!(
    "{} took {}ms",
    context,
    timer.elapsed().as_nanos() as f64 / 1_000_000.0
  );
  *timer = Instant::now();
}

#[windmark::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  std::env::set_var("RUST_LOG", "windmark,locus=trace");
  pretty_env_logger::init();

  let mut time_mount = Instant::now();
  let mut router = windmark::Router::new();

  router.set_private_key_file(".locus/locus_private.pem");
  router.set_certificate_file(".locus/locus_public.pem");
  router.set_error_handler(Box::new(|_| {
    windmark::Response::NotFound(ERROR_HANDLER_RESPONSE.into())
  }));
  router.set_fix_path(true);

  time_section(&mut time_mount, "creating router");

  time_mounts("module", &mut time_mount, || {
    router.attach_stateless(modules::uptime::module);
    router.attach_stateless(modules::sitemap::module);
    router.attach_stateless(modules::search::module);
    router.attach_stateless(modules::remarks::module);
    router.attach_stateless(modules::multi_blog::module);
    router.attach_stateless(modules::random::module);
    router.attach_stateless(modules::r#static::module);
    router.attach_stateless(modules::router::module);
  });

  std::thread::spawn(search::index);

  router.run().await
}