summaryrefslogtreecommitdiff
path: root/angelsharkd/src/main.rs
blob: 8a8a53e0688623409ec73b966859e2c091f9d503 (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
use crate::config::Config;
use anyhow::{Context, Result};
use log::{debug, error, info, LevelFilter};
use tokio::{signal, task};
use warp::{hyper::Method, Filter};

mod config;
mod routes;

#[tokio::main]
async fn main() -> Result<()> {
    // Init config.
    let config = Config::init().with_context(|| "Failed to initialize config.")?;

    // Init logging.
    env_logger::Builder::new()
        .filter(
            None,
            if config.debug_mode {
                LevelFilter::Debug
            } else {
                LevelFilter::Info
            },
        )
        .init();

    if config.debug_mode {
        debug!("**** DEBUGGING MODE ENABLED ****");
    }

    let routes = routes::index()
        .or(routes::ossi(&config))
        .with(if config.debug_mode || config.origin == "*" {
            warp::cors()
                .allow_any_origin()
                .allow_methods(&[Method::GET, Method::POST])
        } else {
            warp::cors()
                .allow_origin(config.origin.as_str())
                .allow_methods(&[Method::GET, Method::POST])
        })
        .with(warp::log("angelsharkd"));

    // Create server with shutdown signal.
    let (addr, server) = warp::serve(routes).bind_with_graceful_shutdown(config.bind_addr, async {
        signal::ctrl_c()
            .await
            .expect("Failed to install CTRL+C signal handler.");
    });

    // Run server to completion.
    info!("Starting server on {} ...", addr);
    if let Err(e) = task::spawn(server).await {
        error!("Server died unexpectedly: {}", e.to_string());
    }
    info!("Stopping server...");

    Ok(())
}