fairings: Failed experiment with templating

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2020-06-28 15:31:18 +05:30
parent 3f5f052a7f
commit 328b14f986
No known key found for this signature in database
GPG Key ID: F475143EDEDEBA3C
6 changed files with 17 additions and 11 deletions

View File

@ -18,3 +18,6 @@ port = 8000
keep_alive = 5 keep_alive = 5
log = "critical" log = "critical"
limits = { forms = 32768 } limits = { forms = 32768 }
#[global]
#template_dir = "resources/templates/"

View File

@ -1,6 +1,6 @@
use rocket::{Request, Response};
use rocket::fairing::{Fairing, Info, Kind}; 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; use std::io::Cursor;
pub struct CORS(); pub struct CORS();
@ -9,14 +9,18 @@ impl Fairing for CORS {
fn info(&self) -> Info { fn info(&self) -> Info {
Info { Info {
name: "Add CORS headers to requests", name: "Add CORS headers to requests",
kind: Kind::Response kind: Kind::Response,
} }
} }
fn on_response(&self, request: &Request, response: &mut 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-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-Headers", "Content-Type"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true")); response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
} }

View File

@ -1,6 +1,6 @@
pub mod catchers; pub mod catchers;
pub mod routes;
pub mod fairings; pub mod fairings;
pub mod routes;
mod guards; mod guards;
mod misc; mod misc;

View File

@ -1,5 +1,5 @@
use rocket::Rocket;
use crate::api::fairings::cors::CORS; use crate::api::fairings::cors::CORS;
use rocket::Rocket;
pub mod health; pub mod health;
pub mod paste; pub mod paste;

View File

@ -1,5 +1,6 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use diesel::result::Error;
use rocket::response::status::Custom; use rocket::response::status::Custom;
use rocket::{ use rocket::{
http::{Cookie, Cookies, Status}, 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::core::users::service::create_or_fetch_user;
use crate::utils::phonetic_key; use crate::utils::phonetic_key;
use diesel::result::Error;
#[post("/", data = "<paste>")] #[post("/", data = "<paste>")]
fn create(mut paste: Json<Paste>, conn: db::DbConn, mut ck: Cookies) -> Custom<Json<Value>> { fn create(mut paste: Json<Paste>, conn: db::DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
// Check if frontend sent a session cookie // Check if frontend sent a session cookie