summaryrefslogtreecommitdiffstats
path: root/service-person/src/main.rs
blob: 60318ca36cf6227bfe0549fbff96c50d585f54b2 (plain)
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
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;

use std::str::FromStr;

use anyhow::Context;
use actix_web::{web, App, HttpServer, HttpResponse, Result};

mod db;
mod model;
mod schema;

use crate::model::*;
use crate::db::DbPool;

embed_migrations!("migrations");

async fn create_person(db: web::Data<DbPool>, person: web::Json<Person>) -> Result<HttpResponse> {
    log::debug!("Creating person = {:?}", person);
    Ok(HttpResponse::Ok().finish())
}


#[actix_web::main]
async fn main() -> anyhow::Result<()> {
    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
    let bind = std::env::var("HOST").expect("environment: HOST variable not set");
    let port = std::env::var("PORT")
        .as_ref()
        .map(|p| u16::from_str(p))
        .expect("environment: PORT variable not set")
        .context("Failed to parse port as integer")?;

    let db_connection_pool = crate::db::establish_connection();

    {
        let conn = db_connection_pool.get().expect("Failed to get connection from pool");
        embedded_migrations::run_with_output(&conn, &mut std::io::stdout())?;
    } // drop `conn` here

    HttpServer::new(move || {
        App::new()
            .data(db_connection_pool.clone())
            .route("/create", web::post().to(create_person))
    })
    .bind(format!("{}:{}", bind, port))?
    .run()
    .await
    .map_err(anyhow::Error::from)
}