From 8a8aae4763e575cfdb268dc602a0cebd9581f138 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 3 Oct 2020 12:31:25 -0400 Subject: started data uri parsing with dichroism --- dichroism/src/main.rs | 132 ++++++++++++++------------------------------------ 1 file changed, 36 insertions(+), 96 deletions(-) (limited to 'dichroism/src') diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index eec1db0..77fd0a1 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -1,122 +1,62 @@ -use actix_web::{guard, web, App, Either, Error, HttpRequest, HttpResponse, HttpServer, Responder}; -use futures::future::{ready, Ready}; +#[macro_use] +extern crate lazy_static; +use actix_web::{get, post, App, HttpResponse, HttpServer, Responder}; +use base64; use listenfd::ListenFd; -use serde::{Deserialize, Serialize}; +use regex::Regex; -#[derive(Serialize)] -struct Product { - id: u32, - name: String, - quantity: i32, - cents: u32, - img_path: String, - description: String, +lazy_static! { + static ref DATA_URI_RE: Regex = + Regex::new("^data:image/(?Ppng|jpg|jpeg);base64,(?P.+)").unwrap(); } -impl Responder for Product { - type Error = Error; - type Future = Ready>; - - fn respond_to(self, _req: &HttpRequest) -> Self::Future { - let body = serde_json::to_string(&self).unwrap(); - - ready(Ok(HttpResponse::Ok() - .content_type("application/json") - .body(body))) - } -} - -#[derive(Deserialize)] -struct NewProduct { - name: String, - quantity: i32, - cents: u32, - img_path: String, - description: String, +#[get("/")] +async fn hello() -> impl Responder { + HttpResponse::Ok().body("Hey, this is an API!") } -type ProductResult = Either>; - -async fn get_single_product(path: web::Path<(u32,)>) -> ProductResult { - let id = path.0; - - if id != 1 { - Either::A(HttpResponse::BadRequest().body("bad data")) +#[post("/products")] +async fn create_product(req_body: String) -> impl Responder { + let caps = if let Some(c) = DATA_URI_RE.captures(&req_body) { + c } else { - Either::B(Ok(Product { - id: 1, - name: "Beach Box".to_string(), - quantity: 3, - cents: 1100, - img_path: "/beach_box.jpg".to_string(), - description: "It's a beach in a box".to_string(), - })) - } -} + return HttpResponse::BadRequest().body("Invalid data URI"); + }; -async fn get_all_products() -> impl Responder { - let product = Product { - id: 1, - name: "Beach Box".to_string(), - quantity: 3, - cents: 1100, - img_path: "/beach_box.jpg".to_string(), - description: "It's a beach in a box".to_string(), + let format = if let Some(f) = caps.name("format") { + f.as_str() + } else { + return HttpResponse::BadRequest().body("Invalid image format."); }; - web::Json(vec![product]) -} + let data = if let Some(d) = caps.name("data") { + d.as_str() + } else { + return HttpResponse::BadRequest().body("No encoded data."); + }; -async fn create_product(new_product: web::Json) -> impl Responder { - Product { - id: 2, - name: new_product.name.to_owned(), - quantity: new_product.quantity, - cents: new_product.cents, - img_path: new_product.img_path.to_owned(), - description: new_product.description.to_owned(), - } + HttpResponse::Ok().body(format!("created\n{}\n{}", format, data)) } -async fn index() -> impl Responder { - "Hello, this is an API. If you're looking for the web app, try the top-level domain at port 443.".to_string() +#[get("/products")] +async fn get_products(_req_body: String) -> impl Responder { + HttpResponse::Ok().body("got product!") } -#[actix_rt::main] +#[actix_web::main] async fn main() -> std::io::Result<()> { let mut listenfd = ListenFd::from_env(); let mut server = HttpServer::new(|| { App::new() - .route("/", web::get().to(index)) - .service( - web::resource("/products") - .route( - web::route() - .guard(guard::Get()) - .guard(guard::Header("content-type", "application/json")) - .to(get_all_products), - ) - .route( - web::route() - .guard(guard::Post()) - .guard(guard::Header("content-type", "application/json")) - .to(create_product), - ), - ) - .service( - web::resource("/products/{id}").route( - web::route() - .guard(guard::Get()) - .guard(guard::Header("content-type", "application/json")) - .to(get_single_product), - ), - ) + .service(hello) + .service(create_product) + .service(get_products) }); - server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { server.listen(l)? } else { - server.bind("127.0.0.1:8000")? + server.bind("127.0.0.1:8080")? }; + server.run().await } -- cgit v1.2.3