Add middleware

This commit is contained in:
Tobias Reisinger 2021-11-06 14:54:45 +01:00
parent 483fd60daa
commit e6278176e4
6 changed files with 16 additions and 13 deletions

BIN
Cargo.lock generated

Binary file not shown.

View file

@ -15,6 +15,7 @@ chrono = { version = "0.4", features = ["serde"] }
diesel = { version = "1.4", features = ["sqlite", "uuid"] } diesel = { version = "1.4", features = ["sqlite", "uuid"] }
diesel_migrations = "1.4" diesel_migrations = "1.4"
dotenv = "0.15" dotenv = "0.15"
env_logger = "0.9.0"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
serde_derive = "1.0" serde_derive = "1.0"

View file

@ -44,10 +44,8 @@ CREATE TABLE schedules
periods BLOB periods BLOB
NOT NULL NOT NULL
); );
--INSERT INTO schedules (uid, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00');
--INSERT INTO schedules (uid, name, periods) VALUES (x'6f6e0000000000000000000000000000', 'on', x'010000009F05');
INSERT INTO schedules (uid, name, periods) VALUES (x'00', 'off', x''); INSERT INTO schedules (uid, name, periods) VALUES (x'00', 'off', x'');
INSERT INTO schedules (uid, name, periods) VALUES (x'01', 'on', x'0000173B'); INSERT INTO schedules (uid, name, periods) VALUES (x'01', 'on', x'00000000');
CREATE TABLE tags CREATE TABLE tags
( (

View file

@ -15,7 +15,7 @@ use crate::types::EmgauwaUid;
pub struct Schedule { pub struct Schedule {
#[serde(skip)] #[serde(skip)]
pub id: i32, pub id: i32,
#[serde(alias = "id")] #[serde(rename(serialize = "id"))]
pub uid: EmgauwaUid, pub uid: EmgauwaUid,
pub name: String, pub name: String,
pub periods: Periods, pub periods: Periods,

View file

@ -45,17 +45,10 @@ pub async fn show(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Resp
} }
pub async fn add(post: web::Json<RequestSchedule>) -> impl Responder { pub async fn add(post: web::Json<RequestSchedule>) -> 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); let new_schedule = db::create_schedule(&post.name, &post.periods);
match new_schedule { match new_schedule {
Ok(ok) => HttpResponse::Ok().json(ok), Ok(ok) => HttpResponse::Created().json(ok),
Err(err) => HttpResponse::InternalServerError().json(err), Err(err) => HttpResponse::InternalServerError().json(err),
} }
} }

View file

@ -4,7 +4,9 @@ extern crate diesel;
extern crate diesel_migrations; extern crate diesel_migrations;
extern crate dotenv; 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 db;
mod handlers; mod handlers;
@ -14,8 +16,17 @@ mod types;
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
db::run_migrations(); db::run_migrations();
Builder::from_env(Env::default().default_filter_or("info")).init();
HttpServer::new(|| { HttpServer::new(|| {
App::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( .route(
"/api/v1/schedules", "/api/v1/schedules",
web::get().to(handlers::v1::schedules::index), web::get().to(handlers::v1::schedules::index),