summaryrefslogblamecommitdiff
path: root/src/main.rs
blob: a2af4554c56d35fee009ca8f6885a5ab07c7b79f (plain) (tree)
1
2
3
4
5
6
7
8
9




                                           
 
           
             
           

              
              

                                                           
 




                                                                                




                                                        
       
 
                                                     

                                                                    
                                


                                         

          

 
                            
                      



                                                           
use hyper::{
    service::{make_service_fn, service_fn},
    Server,
};
use std::convert::Infallible; // TODO:

mod config;
mod handlers;
mod models;
mod templates;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = config::INSTANCE.addr;

    let client = mongodb::Client::with_uri_str(&config::INSTANCE.db_uri).await?;

    let make_svc = make_service_fn(move |_conn| {
        let client = client.clone();

        async {
            Ok::<_, Infallible>(service_fn(move |req| {
                handlers::router(req, client.to_owned())
            }))
        }
    });

    let server = Server::bind(&addr).serve(make_svc);
    let graceful = server.with_graceful_shutdown(shutdown_signal());

    // run until shutdown signal
    if let Err(e) = graceful.await {
        eprintln!("server error: {}", e);
    }

    Ok(())
}

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