From f8bf353073220ce329d8eb347e3574d5793b6d26 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 3 Oct 2020 22:39:05 -0400 Subject: moved handlers into separate module, started config --- dichroism/src/config.rs | 1 + dichroism/src/handlers.rs | 29 +++++++++++++++++++++++++++++ dichroism/src/main.rs | 37 ++++++------------------------------- 3 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 dichroism/src/config.rs create mode 100644 dichroism/src/handlers.rs diff --git a/dichroism/src/config.rs b/dichroism/src/config.rs new file mode 100644 index 0000000..e5b4046 --- /dev/null +++ b/dichroism/src/config.rs @@ -0,0 +1 @@ +struct Config {} diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs new file mode 100644 index 0000000..1e0ae28 --- /dev/null +++ b/dichroism/src/handlers.rs @@ -0,0 +1,29 @@ +use crate::image_api; +use actix_web::{get, post, HttpResponse, Responder}; + +#[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 { + let data = match image_api::extract_data(&req_body) { + Err(e) => return HttpResponse::BadRequest().body(format!("fail: {}", e.to_string())), + Ok(d) => d, + }; + + if let Err(e) = image_api::generate_images(data) { + 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!") +} diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index f832121..442b92d 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -1,48 +1,23 @@ #[macro_use] extern crate lazy_static; -use actix_web::{get, post, App, HttpResponse, HttpServer, Responder}; +use actix_web::{App, HttpServer}; use listenfd::ListenFd; +mod config; mod error; +mod handlers; 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 { - let data = match image_api::extract_data(&req_body) { - Err(e) => return HttpResponse::BadRequest().body(format!("fail: {}", e.to_string())), - Ok(d) => d, - }; - - if let Err(e) = image_api::generate_images(data) { - 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) + .service(handlers::hello) + .service(handlers::create_image) + .service(handlers::get_products) }); server = if let Some(l) = listenfd -- cgit v1.2.3