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




                    
                                 
                                      



                                      



                   
         

             
           
         


           


                               
                                     
                                                                             




                                               

                                     


                                              

       

                                                                                               




                                               

                         
                                      



                           
#[macro_use]
extern crate serde;
#[macro_use]
extern crate diesel;

use actix_web::{App, HttpServer};
use config::CONFIG_INSTANCE as CONFIG;
use diesel::prelude::SqliteConnection;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::Pool;
use listenfd::ListenFd;
use result::Result;

mod config;
mod constants;
mod dtos;
mod error;
mod handlers;
mod models;
mod repo;
mod result;
mod schema;
mod types;

#[actix_web::main]
async fn main() -> Result<()> {
    // Initialize DB connection pool.
    let manager = ConnectionManager::<SqliteConnection>::new(&CONFIG.db_url);
    let pool = Pool::builder().build(manager)?;

    // Initialize application server.
    let mut server = HttpServer::new(move || {
        App::new()
            .data(pool.clone())
            .service(handlers::hello)
            .service(handlers::get_products)
            .service(handlers::update_product)
            .service(handlers::create_product)
    });

    // If using listenfd, bind to it instead of the configured address to allow for cargo watch
    // auto-reloading
    let mut listenfd = ListenFd::from_env();
    server = if let Some(l) = listenfd
        .take_tcp_listener(0)
        .expect("Unable to grab TCP listener!")
    {
        server.listen(l)?
    } else {
        server.bind(CONFIG.bind_addr)?
    };

    Ok(server.run().await?)
}