aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 57b227fbe71c5e35d41fff6ddf4a55e842da5bbf (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
// 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, const_extern_fn)]
#![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 xml;

#[macro_use]
extern crate log;

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

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

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

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();
  dotenv::dotenv().ok();

  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, || stateless!(router, modules));

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

  router.run().await
}