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_migrations = "1.4"
dotenv = "0.15"
env_logger = "0.9.0"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"

View file

@ -44,10 +44,8 @@ CREATE TABLE schedules
periods BLOB
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'01', 'on', x'0000173B');
INSERT INTO schedules (uid, name, periods) VALUES (x'01', 'on', x'00000000');
CREATE TABLE tags
(

View file

@ -15,7 +15,7 @@ use crate::types::EmgauwaUid;
pub struct Schedule {
#[serde(skip)]
pub id: i32,
#[serde(alias = "id")]
#[serde(rename(serialize = "id"))]
pub uid: EmgauwaUid,
pub name: String,
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 {
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),
}
}

View file

@ -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),