summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: c6a11101c5b68a20bc43cec2a26881f88c04bb1e (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
use crate::error::TwinHError;
use hyper::{
    service::{make_service_fn, service_fn},
    Server,
};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use structopt::StructOpt;

#[macro_use]
extern crate lazy_static;

mod config;
mod error;
mod import;
mod models;
mod repo;
mod routes;
mod templates;

#[tokio::main]
async fn main() -> Result<(), TwinHError> {
    //env_logger::try_init_from_env;

    // create HTTP listener
    let make_svc =
        make_service_fn(move |_conn| async { Ok::<_, TwinHError>(service_fn(routes::router)) });

    // bind server with signal
    let server = Server::bind(&config::INSTANCE.bind_addr.into()).serve(make_svc);
    let graceful = server.with_graceful_shutdown(shutdown_signal());

    // start and run
    graceful.await?;

    Ok(())
}

async fn shutdown_signal() {
    // Wait for CTRL+C
    tokio::signal::ctrl_c()
        .await
        .expect("failed to install CTRL+C signal handler");
}