cdb/src/main.rs

162 lines
6.1 KiB
Rust
Raw Normal View History

2024-09-12 08:34:14 +00:00
use flow::FlowInfo;
2024-09-02 16:40:02 +00:00
use json_store::JSONStore;
use location::Location;
2024-09-03 09:16:45 +00:00
2024-06-21 19:14:45 +00:00
use rocket::routes as route;
use rocket::{http::Method, launch};
2024-10-07 18:53:58 +00:00
use tokio::sync::OnceCell;
2024-01-16 08:02:23 +00:00
2024-09-08 00:37:39 +00:00
mod config;
2024-05-03 16:22:59 +00:00
mod db;
2024-09-09 19:49:36 +00:00
mod flow;
2024-09-19 09:42:59 +00:00
mod integrity;
2024-01-14 02:57:13 +00:00
mod item;
2024-09-02 16:40:02 +00:00
mod json_store;
mod location;
2024-04-04 05:41:13 +00:00
mod routes;
2024-05-03 16:22:59 +00:00
mod transaction;
mod variant;
2024-01-14 02:57:13 +00:00
2024-10-07 18:53:58 +00:00
pub static PG: OnceCell<sqlx::PgPool> = OnceCell::const_new();
#[macro_export]
macro_rules! get_pg {
() => {
if let Some(client) = $crate::PG.get() {
client
} else {
let client = sqlx::postgres::PgPoolOptions::new()
.max_connections(5)
.connect(&std::env::var("DATABASE_URL").unwrap())
.await
.unwrap();
$crate::PG.set(client).unwrap();
$crate::PG.get().unwrap()
}
};
}
pub static ITEMDB: OnceCell<db::ItemDB> = OnceCell::const_new();
#[macro_export]
macro_rules! get_itemdb {
() => {
if let Some(client) = $crate::ITEMDB.get() {
client
} else {
let itemdb = $crate::db::ItemDB::new("./itemdb").await;
$crate::ITEMDB.set(itemdb).unwrap();
$crate::ITEMDB.get().unwrap()
}
};
}
pub static LOCATIONS: OnceCell<JSONStore<Location>> = OnceCell::const_new();
#[macro_export]
macro_rules! get_locations {
() => {
if let Some(client) = $crate::LOCATIONS.get() {
client
} else {
2024-10-07 19:15:57 +00:00
let mut locations: $crate::json_store::JSONStore<$crate::location::Location> =
$crate::JSONStore::new("./locations");
let loc_keys: Vec<_> = locations.keys().cloned().collect();
for loc in loc_keys {
let location = locations.get_mut(&loc).unwrap();
location.id = loc.clone();
}
2024-10-07 18:53:58 +00:00
$crate::LOCATIONS.set(locations).unwrap();
$crate::LOCATIONS.get().unwrap()
}
};
}
2024-01-14 02:57:13 +00:00
// ░░░░░░░░░░▀▀▀██████▄▄▄░░░░░░░░░░
// ░░░░░░░░░░░░░░░░░▀▀▀████▄░░░░░░░
// ░░░░░░░░░░▄███████▀░░░▀███▄░░░░░
// ░░░░░░░░▄███████▀░░░░░░░▀███▄░░░
// ░░░░░░▄████████░░░░░░░░░░░███▄░░
2024-01-16 08:02:23 +00:00
// ░░░░░██████████▄░░░░░░░░░░░███▌░ ▒█▀▀█ █▀▀█ █▀▄▀█ █▀▄▀█ ▒█▀▀▄ ▒█▀▀█
// ░░░░░▀█████▀░▀███▄░░░░░░░░░▐███░ ▒█░░░ █░░█ █░▀░█ █░▀░█ ▒█░▒█ ▒█▀▀▄
2024-01-14 02:57:13 +00:00
// ░░░░░░░▀█▀░░░░░▀███▄░░░░░░░▐███░ ▒█▄▄█ ▀▀▀▀ ▀░░░▀ ▀░░░▀ ▒█▄▄▀ ▒█▄▄█
// ░░░░░░░░░░░░░░░░░▀███▄░░░░░███▌░
// ░░░░▄██▄░░░░░░░░░░░▀███▄░░▐███░░
// ░░▄██████▄░░░░░░░░░░░▀███▄███░░░
// ░█████▀▀████▄▄░░░░░░░░▄█████░░░░
// ░████▀░░░▀▀█████▄▄▄▄█████████▄░░
// ░░▀▀░░░░░░░░░▀▀██████▀▀░░░▀▀██░░
2024-06-21 19:14:45 +00:00
#[launch]
async fn rocket() -> _ {
2024-08-28 20:03:39 +00:00
env_logger::init();
2024-06-21 19:14:45 +00:00
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");
2024-01-14 02:57:13 +00:00
2024-10-07 19:15:57 +00:00
let pg = get_pg!();
sqlx::migrate!("./migrations").run(pg).await.unwrap();
2024-09-19 09:42:59 +00:00
let config = config::get_config();
2024-10-07 19:02:23 +00:00
let itemdb = get_itemdb!().clone();
let locations = get_locations!().clone();
2024-10-07 18:53:58 +00:00
let flows: JSONStore<FlowInfo> = JSONStore::new("./flows");
2024-09-19 09:42:59 +00:00
integrity::verify_integrity(&config, &flows, &locations, &itemdb).await;
2024-09-02 16:40:02 +00:00
2024-06-21 19:14:45 +00:00
rocket::build()
.mount(
"/",
route![
routes::item::get_items_route,
2024-06-22 00:05:22 +00:00
routes::item::item_route,
2024-06-21 19:14:45 +00:00
routes::item::item_variants_page,
routes::item::supply_log_route,
routes::item::demand_log_route,
routes::item::supply_route,
routes::item::demand_route,
2024-08-13 02:13:38 +00:00
routes::item::transaction_route,
2024-08-27 21:33:09 +00:00
routes::item::inventory_route,
2024-09-05 08:33:34 +00:00
routes::item::inventory_route_variant,
2024-08-28 20:03:39 +00:00
routes::item::variant_stat_route,
2024-09-02 16:43:42 +00:00
routes::item::unique_field_route,
routes::item::location_info,
2024-09-05 08:33:34 +00:00
routes::item::locations_info,
2024-09-12 08:34:14 +00:00
routes::item::locations_list,
routes::item::location_inventory,
routes::flow::flow_info,
2024-09-12 08:58:49 +00:00
routes::flow::flows_list,
routes::item::expired_items_route,
2024-09-12 11:11:44 +00:00
routes::item::min_items_route,
2024-09-13 12:17:55 +00:00
routes::item::variant_price_history_by_origin,
routes::flow::end_flow_route,
routes::flow::continue_flow_route,
2024-09-13 20:33:21 +00:00
routes::flow::create_flow_route,
2024-09-16 06:10:26 +00:00
routes::item::move_transaction_route,
2024-09-20 10:53:22 +00:00
routes::item::variant_price_latest_by_origin,
2024-09-21 15:16:24 +00:00
routes::item::item_stat_route,
2024-09-23 14:37:18 +00:00
routes::flow::active_flows_route,
2024-09-25 06:38:12 +00:00
routes::flow::flow_api_route,
routes::flow::create_flow_note_route,
routes::flow::flow_notes_route
2024-06-21 19:14:45 +00:00
],
)
.manage(itemdb)
2024-09-02 16:40:02 +00:00
.manage(locations)
2024-09-12 08:34:14 +00:00
.manage(flows)
2024-09-08 00:37:39 +00:00
.manage(config)
2024-06-21 19:14:45 +00:00
.attach(cors)
2024-01-14 02:57:13 +00:00
}