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

use actix_web::{get, post, App, HttpResponse, HttpServer, Responder};
use listenfd::ListenFd;

mod image_api;
mod result;

#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hey, this is an API!")
}

#[post("/images")]
async fn create_image(req_body: String) -> impl Responder {
    if let Err(e) = image_api::data_uri_to_files(&req_body) {
        return HttpResponse::BadRequest().body(format!(
            "Unable to extract image from data URI: {}",
            e.to_string()
        ));
    }

    HttpResponse::Ok().body("Image created.")
}

#[get("/products")]
async fn get_products(_req_body: String) -> impl Responder {
    HttpResponse::Ok().body("got products!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let mut listenfd = ListenFd::from_env();
    let mut server = HttpServer::new(|| {
        App::new()
            .service(hello)
            .service(create_image)
            .service(get_products)
    });

    server = if let Some(l) = listenfd
        .take_tcp_listener(0)
        .expect("Unable to grab TCP listener!")
    {
        // "Debug mode" with cargo watch auto-reloading
        server.listen(l)?
    } else {
        // "Release mode"
        server.bind("127.0.0.1:8000")?
    };

    server.run().await
}