From e6278176e483124f48752b21b9fb07c3658cd824 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sat, 6 Nov 2021 14:54:45 +0100 Subject: [PATCH] Add middleware --- Cargo.lock | Bin 48272 -> 49456 bytes Cargo.toml | 1 + migrations/2021-10-13-000000_init/up.sql | 4 +--- src/db/models.rs | 2 +- src/handlers/v1/schedules.rs | 9 +-------- src/main.rs | 13 ++++++++++++- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37f426231aaf9ca4591d40c47ac8ba520b71c1c5..e8a9666960442d71b86275dbd96a5fb019a51d88 100644 GIT binary patch delta 544 zcmX|;KWmgh5XRvS)Lbj(geaZ|f{4h@&i+l*oB_eYMzNQj-P!XX_a3}CgOWRDe|Wju8ot~ZaI^n#cQKuE z+kKKZd#C=*<=wNmEl9>JP$_SL*+iov0A$@YyU4wn1zWC7&K)9 zuRT^fglI?xODffzDR6YEqOiHbNT|>&Bfz4q8x%CFa`C~iyLET#&e^|0?Rc=y?cQwB zjBbp^a1F}WbN42{vn1zz7EGe@loeYvL4-hwT(DkwqdlWCMjOacgp{>nX_9kMXSG7y z9G@s3HZODsZ(m$0-?mSeFK_Notl(%OJ52=Ifrc1ZvyQG>*1@s2Nydthga9e4GzLV) X7+EI<`{#Mh@mhKH{(SfS<6-X?oUN+u delta 51 zcmV-30L=fef&-BG0) -> impl Resp } pub async fn add(post: web::Json) -> impl Responder { - - println!("model: {:?}", post); - - for period in post.periods.0.iter() { - println!("start: {:?}; end: {:?}", period.start, period.end); - } - let new_schedule = db::create_schedule(&post.name, &post.periods); match new_schedule { - Ok(ok) => HttpResponse::Ok().json(ok), + Ok(ok) => HttpResponse::Created().json(ok), Err(err) => HttpResponse::InternalServerError().json(err), } } diff --git a/src/main.rs b/src/main.rs index 65f8cb0..d5e5575 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,9 @@ extern crate diesel; extern crate diesel_migrations; extern crate dotenv; -use actix_web::{App, HttpServer, web}; +use actix_web::{middleware, web, App, HttpServer}; +use actix_web::middleware::normalize::TrailingSlash; +use env_logger::{Builder, Env}; mod db; mod handlers; @@ -14,8 +16,17 @@ mod types; async fn main() -> std::io::Result<()> { db::run_migrations(); + Builder::from_env(Env::default().default_filter_or("info")).init(); + HttpServer::new(|| { App::new() + .wrap(middleware::DefaultHeaders::new() + .header("Access-Control-Allow-Origin", "*") + .header("Access-Control-Allow-Headers", "*") + .header("Access-Control-Allow-Methods", "*") + ) + .wrap(middleware::Logger::default()) + .wrap(middleware::NormalizePath::new(TrailingSlash::Trim)) .route( "/api/v1/schedules", web::get().to(handlers::v1::schedules::index),