aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl/src/lib.rs
blob: 356a94021dd9e18a4cbfe43221afffce03edb99c (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
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

#![feature(type_ascription, hash_set_entry, decl_macro, proc_macro_hygiene)]
#![deny(
  warnings,
  nonstandard_style,
  unused,
  future_incompatible,
  rust_2018_idioms,
  unsafe_code
)]
#![deny(clippy::all, clippy::nursery, clippy::pedantic)]
#![recursion_limit = "128"]
#![doc(
  html_logo_url = "https://raw.githubusercontent.com/Whirlsplash/assets/master/Whirl.png",
  html_favicon_url = "https://raw.githubusercontent.com/Whirlsplash/assets/master/Whirl.png"
)]

#[macro_use] extern crate log;
// #[macro_use]
// extern crate simple_error;

// #[cfg(windows)]
// #[global_allocator]
// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

// #[cfg(unix)]
// #[global_allocator]
// static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;

pub mod cli;

#[cfg(unix)] use signal_hook::consts::signal::{SIGINT, SIGTERM};
use whirl_config::Config;

pub struct Whirl;
impl Whirl {
  /// # Errors
  /// if the Logger fails to start.
  ///
  /// # Panics
  /// if the Logger fails to be created.
  pub async fn splash() -> Result<(), Box<dyn std::error::Error>> {
    // Environment
    std::env::set_var("DATABASE_URL", ".whirl/db.sqlite3");

    // Logging
    dotenv::dotenv().ok();
    human_panic::setup_panic!();
    if Config::get().whirlsplash.log.enable {
      let logger = flexi_logger::Logger::try_with_str(
        whirl_common::log::calculate_log_level(),
      )
      .unwrap();
      if std::env::var("LOG_FILE").unwrap_or_else(|_| "true".to_string())
        == "false"
        || !whirl_config::Config::get().whirlsplash.log.file
        || ({
          // Cheeky as all hell.
          let args = std::env::args().collect::<Vec<_>>();
          if args.len() == 2 { args[1] == "clean" } else { false }
        })
      {
        logger.start()?;
      } else {
        logger
          .print_message()
          .log_to_file(
            flexi_logger::FileSpec::default().directory(".whirl/log"),
          )
          .start()?;
      }
    }

    // Ctrl+C handling
    #[cfg(unix)]
    #[allow(clippy::never_loop)]
    tokio::spawn(async move {
      for signal in signal_hook::iterator::Signals::new([SIGTERM, SIGINT])
        .unwrap()
        .forever()
      {
        info!("signal received: {:?}, killing whirl", signal);
        std::process::exit(0);
      }
    });

    crate::cli::Cli::execute().await;

    Ok(())
  }
}