move to rocket + refactor

This commit is contained in:
JMARyA 2024-06-21 21:14:45 +02:00
parent b0e80dd3e8
commit b8caa43972
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
11 changed files with 686 additions and 886 deletions

View file

@ -1,4 +1,5 @@
use actix_web::{get, HttpRequest, Responder};
use rocket::routes as route;
use rocket::{http::Method, launch};
mod cache;
mod db;
@ -22,31 +23,35 @@ mod variant;
// ░████▀░░░▀▀█████▄▄▄▄█████████▄░░
// ░░▀▀░░░░░░░░░▀▀██████▀▀░░░▀▀██░░
#[get("/")]
pub(crate) async fn index(r: HttpRequest) -> impl Responder {
let itemdb: &actix_web::web::Data<db::ItemDB> = r.app_data().unwrap();
let content = "";
web_base::func::build_site(&r, "Index", &content)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
#[launch]
async fn rocket() -> _ {
let cors = rocket_cors::CorsOptions {
allowed_origins: rocket_cors::AllowedOrigins::all(),
allowed_methods: vec![Method::Get, Method::Post, Method::Options]
.into_iter()
.map(From::from)
.collect(),
allowed_headers: rocket_cors::AllowedHeaders::all(),
allow_credentials: true,
..Default::default()
}
.to_cors()
.expect("error creating CORS options");
let itemdb = db::ItemDB::new("./itemdb").await;
let itemdb = actix_web::web::Data::new(itemdb);
web_base::map!(web_base::Site::new(), |app: actix_web::App<_>| {
app.app_data(itemdb.clone())
.service(index)
.service(routes::item::supply_route) // /supply
.service(routes::item::item_variants_page) // /item/{item_id}/variants
.service(routes::item::get_items_route) // /items
.service(routes::item::demand_route) // /demand
.service(routes::item::supply_log_route) // /item/{item_id}/{variant_id}/supply
.service(routes::item::demand_log_route) // /item/{item_id}/{variant_id}/demand
})
.bind(("0.0.0.0".to_string(), 8080))?
.run()
.await
rocket::build()
.mount(
"/",
route![
routes::item::get_items_route,
routes::item::item_variants_page,
routes::item::supply_log_route,
routes::item::demand_log_route,
routes::item::supply_route,
routes::item::demand_route,
],
)
.manage(itemdb)
.attach(cors)
}