summaryrefslogtreecommitdiff
path: root/dichroism/src/main.rs
blob: 6c76f0ab44f7d1f5c046ca1ef1504de9bd3bfd71 (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
60
61
62
63
64
65
66
#[macro_use]
extern crate serde;
#[macro_use]
extern crate diesel;

use actix_cors::Cors;
use actix_web::{middleware::Logger, 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 image_service;
mod models;
mod repo;
mod result;
mod schema;
mod types;

#[actix_web::main]
async fn main() -> Result<()> {
    // Init logging
    std::env::set_var("RUST_LOG", "actix_web=info");
    env_logger::init();

    // Init DB connection pool
    let manager = ConnectionManager::<SqliteConnection>::new(&CONFIG.db_url);
    let pool = Pool::builder().build(manager)?;

    // Init application server
    let mut server = HttpServer::new(move || {
        // Init CORS policy
        let cors = Cors::permissive();

        App::new()
            .data(pool.clone())
            .wrap(cors)
            .wrap(Logger::default())
            .service(handlers::hello)
            .service(handlers::get_products)
            .service(handlers::patch_product)
            .service(handlers::post_product)
            .service(handlers::post_photo)
    });

    // 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?)
}