cdb/src/main.rs

186 lines
6.7 KiB
Rust
Raw Normal View History

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