#[macro_use] extern crate rocket; use rocket::fairing::{Fairing, Info, Kind}; use rocket::fs::FileServer; use rocket::{http::Header, Request, Response}; #[macro_use] extern crate paris; mod api; mod authenticate; mod captcha; mod database; mod html; mod structures; use crate::api::*; use crate::captcha::*; use argon2::{ password_hash::{rand_core::OsRng, SaltString}, Argon2, }; pub struct CORS; // Setup CORS header #[rocket::async_trait] impl Fairing for CORS { fn info(&self) -> Info { Info { name: "Attaching CORS headers to responses", kind: Kind::Response, } } async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) { response.set_header(Header::new("Access-Control-Allow-Origin", "*")); response.set_header(Header::new( "Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS", )); response.set_header(Header::new("Access-Control-Allow-Headers", "*")); response.set_header(Header::new("Access-Control-Allow-Credentials", "true")); } } // Launch rocket #[launch] fn rocket() -> _ { let log = paris::Logger::new(); html::render_pages(); info!("Starting up Rocket"); let _config = sled::Config::default() .use_compression(true) .compression_factor(15); rocket::build() .manage(Argon2::default()) // Manage Argon2 state .manage(SaltString::generate(&mut OsRng)) // Manage RNG state .mount( "/", routes![ set_password, new_order, update_order, order_info, all_orders, new_product, update_product, return_captcha, check_captcha, get_product_info, delete_order ], ) .mount("/", FileServer::from("static/")) .attach(CORS) }