summaryrefslogtreecommitdiff
path: root/dichroism/src/main.rs
blob: 57ac6520bdbec149480b5a237621f7906c40a2b6 (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
#[macro_use]
extern crate serde;
#[macro_use]
extern crate diesel;

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

mod config;
mod constants;
mod error;
mod handlers;
mod image_api;
mod image_repo;
mod models;
mod product_repo;
mod result;
mod schema;
mod types;

#[actix_web::main]
async fn main() -> Result<()> {
    // Gather config.
    let config = Config::from_toml()?;
    let bind_addr = config.bind_addr;

    // 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(config.clone())
            .data(pool.clone())
            .service(handlers::hello)
            .service(handlers::create_image)
            .service(handlers::get_images)
    });

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

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