#![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; #[macro_use] extern crate rocket_contrib; #[macro_use] extern crate diesel_migrations; #[cfg(test)] mod tests; mod catchers; mod routes; // Very temporary solution for CORS // https://stackoverflow.com/questions/62412361/how-to-set-up-cors-or-options-for-rocket-rs use rocket::fairing::AdHoc; use rocket::fairing::{Fairing, Info, Kind}; use rocket::http::Header; use rocket::{Request, Response, Rocket}; use rocket_contrib::databases::diesel; pub struct CORS; impl Fairing for CORS { fn info(&self) -> Info { Info { name: "Add CORS headers to responses", kind: Kind::Response, } } fn on_response(&self, _: &Request, response: &mut Response) { 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")); } } // Macro from diesel_migrations that sets up migrations embed_migrations!(); // This defines a connection to the database #[database("postgres_fej")] pub struct FejDbConn(diesel::PgConnection); // I'd like to thank Stackoverflow for helping me with this // https://stackoverflow.com/questions/61047355/how-to-run-diesel-migration-with-rocket-in-production fn run_db_migrations(rocket: Rocket) -> Result { let conn = FejDbConn::get_one(&rocket).expect("database connection"); match embedded_migrations::run(&*conn) { Ok(()) => Ok(rocket), Err(e) => Err(rocket), } } fn rocket() -> rocket::Rocket { rocket::ignite() .attach(CORS) .attach(FejDbConn::fairing()) .attach(AdHoc::on_attach("Database Migrations", run_db_migrations)) .mount("/ivago", routes::ivago()) .register(catchers![catchers::not_found]) } fn main() { rocket().launch(); }