summaryrefslogtreecommitdiffstats
path: root/service-person/src/main.rs
blob: 81be6741b9c6991cbd587fce16fbf9e5560e80b2 (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
use std::str::FromStr;

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

#[derive(Debug, Deserialize)]
struct Name(String);

#[derive(Debug, Deserialize)]
struct Age(usize);

#[derive(Debug, Deserialize)]
struct Address {
    street: String,
    number: String,
    city: String,
    country: String,
}

#[derive(Debug, Deserialize)]
struct Person {
    name: Name,
    age: Age,
    addr: Address,
}

async fn create_person(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")?;

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