From 328b14f986a96d54aff6a7ca80a4391c099151f0 Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Sun, 28 Jun 2020 15:31:18 +0530 Subject: [PATCH] fairings: Failed experiment with templating Signed-off-by: ATechnoHazard --- Rocket.toml | 3 +++ src/api/fairings/cors.rs | 16 ++++++++++------ src/api/fairings/mod.rs | 2 +- src/api/mod.rs | 2 +- src/api/routes/mod.rs | 2 +- src/api/routes/paste.rs | 3 +-- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Rocket.toml b/Rocket.toml index e9368db..195033a 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -18,3 +18,6 @@ port = 8000 keep_alive = 5 log = "critical" limits = { forms = 32768 } + +#[global] +#template_dir = "resources/templates/" diff --git a/src/api/fairings/cors.rs b/src/api/fairings/cors.rs index 2bd70d2..4d8a4b9 100644 --- a/src/api/fairings/cors.rs +++ b/src/api/fairings/cors.rs @@ -1,6 +1,6 @@ -use rocket::{Request, Response}; use rocket::fairing::{Fairing, Info, Kind}; -use rocket::http::{Header, ContentType, Method}; +use rocket::http::{ContentType, Header, Method}; +use rocket::{Request, Response}; use std::io::Cursor; pub struct CORS(); @@ -9,14 +9,18 @@ impl Fairing for CORS { fn info(&self) -> Info { Info { name: "Add CORS headers to requests", - kind: Kind::Response + kind: Kind::Response, } } fn on_response(&self, request: &Request, response: &mut Response) { - if request.method() == Method::Options || response.content_type() == Some(ContentType::JSON) { + if request.method() == Method::Options || response.content_type() == Some(ContentType::JSON) + { response.set_header(Header::new("Access-Control-Allow-Origin", "*")); - response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET, OPTIONS")); + response.set_header(Header::new( + "Access-Control-Allow-Methods", + "POST, GET, OPTIONS", + )); response.set_header(Header::new("Access-Control-Allow-Headers", "Content-Type")); response.set_header(Header::new("Access-Control-Allow-Credentials", "true")); } @@ -26,4 +30,4 @@ impl Fairing for CORS { response.set_sized_body(Cursor::new("")); } } -} \ No newline at end of file +} diff --git a/src/api/fairings/mod.rs b/src/api/fairings/mod.rs index 23b2775..3bd498a 100644 --- a/src/api/fairings/mod.rs +++ b/src/api/fairings/mod.rs @@ -1 +1 @@ -pub mod cors; \ No newline at end of file +pub mod cors; diff --git a/src/api/mod.rs b/src/api/mod.rs index 0cd0eba..8387032 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,6 +1,6 @@ pub mod catchers; -pub mod routes; pub mod fairings; +pub mod routes; mod guards; mod misc; diff --git a/src/api/routes/mod.rs b/src/api/routes/mod.rs index 0ca44e2..3a58f5f 100644 --- a/src/api/routes/mod.rs +++ b/src/api/routes/mod.rs @@ -1,5 +1,5 @@ -use rocket::Rocket; use crate::api::fairings::cors::CORS; +use rocket::Rocket; pub mod health; pub mod paste; diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index 107c973..bc19312 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -1,5 +1,6 @@ use std::ops::DerefMut; +use diesel::result::Error; use rocket::response::status::Custom; use rocket::{ http::{Cookie, Cookies, Status}, @@ -14,8 +15,6 @@ use crate::core::paste::{entity::Paste, service::create_paste, service::fetch_pa use crate::core::users::service::create_or_fetch_user; use crate::utils::phonetic_key; -use diesel::result::Error; - #[post("/", data = "")] fn create(mut paste: Json, conn: db::DbConn, mut ck: Cookies) -> Custom> { // Check if frontend sent a session cookie