pharmacy/src/main.rs

83 lines
1.9 KiB
Rust
Raw Normal View History

2021-11-10 20:28:08 +00:00
#[macro_use]
extern crate rocket;
use rocket::fairing::{Fairing, Info, Kind};
2022-04-01 02:49:17 +00:00
use rocket::fs::FileServer;
use rocket::{http::Header, Request, Response};
2021-11-10 20:28:08 +00:00
#[macro_use]
extern crate paris;
2021-11-10 20:28:08 +00:00
mod api;
mod authenticate;
mod captcha;
2021-11-10 20:28:08 +00:00
mod database;
2022-04-01 02:49:17 +00:00
mod html;
2021-11-10 20:28:08 +00:00
mod structures;
use crate::api::*;
use crate::captcha::*;
2021-11-10 20:28:08 +00:00
use argon2::{
password_hash::{rand_core::OsRng, SaltString},
Argon2,
};
2021-11-10 20:28:08 +00:00
pub struct CORS;
// Setup CORS header
2021-11-10 20:28:08 +00:00
#[rocket::async_trait]
impl Fairing for CORS {
fn info(&self) -> Info {
Info {
name: "Attaching CORS headers to responses",
kind: Kind::Response,
2021-11-10 20:28:08 +00:00
}
}
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",
));
2021-11-10 20:28:08 +00:00
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}
// Launch rocket
2021-11-10 20:28:08 +00:00
#[launch]
fn rocket() -> _ {
2021-11-23 16:55:00 +00:00
let log = paris::Logger::new();
2022-04-01 02:49:17 +00:00
html::render_pages();
info!("Starting up Rocket");
let _config = sled::Config::default()
.use_compression(true)
.compression_factor(15);
2021-11-10 20:28:08 +00:00
rocket::build()
.manage(Argon2::default()) // Manage Argon2 state
.manage(SaltString::generate(&mut OsRng)) // Manage RNG state
.mount(
"/",
routes![
2021-11-23 16:55:00 +00:00
set_password,
new_order,
update_order,
order_info,
all_orders,
new_product,
update_product,
return_captcha,
check_captcha,
get_product_info,
delete_order
],
)
2022-04-01 02:49:17 +00:00
.mount("/", FileServer::from("static/"))
2021-11-10 20:28:08 +00:00
.attach(CORS)
}