postgres
This commit is contained in:
parent
584ffb6b11
commit
1faa3b9668
19 changed files with 1058 additions and 1244 deletions
64
src/main.rs
64
src/main.rs
|
@ -4,6 +4,7 @@ use location::Location;
|
|||
|
||||
use rocket::routes as route;
|
||||
use rocket::{http::Method, launch};
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
mod config;
|
||||
mod db;
|
||||
|
@ -16,6 +17,55 @@ mod routes;
|
|||
mod transaction;
|
||||
mod variant;
|
||||
|
||||
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 {
|
||||
let locations = $crate::JSONStore::new("./locations");
|
||||
$crate::LOCATIONS.set(locations).unwrap();
|
||||
$crate::LOCATIONS.get().unwrap()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// ░░░░░░░░░░▀▀▀██████▄▄▄░░░░░░░░░░
|
||||
// ░░░░░░░░░░░░░░░░░▀▀▀████▄░░░░░░░
|
||||
// ░░░░░░░░░░▄███████▀░░░▀███▄░░░░░
|
||||
|
@ -49,19 +99,11 @@ async fn rocket() -> _ {
|
|||
.expect("error creating CORS options");
|
||||
|
||||
let config = config::get_config();
|
||||
let itemdb = db::ItemDB::new("./itemdb").await;
|
||||
let mut locations: JSONStore<Location> = JSONStore::new("./locations");
|
||||
let mut flows: JSONStore<FlowInfo> = JSONStore::new("./flows");
|
||||
let itemdb = get_itemdb!();
|
||||
let locations = get_locations!();
|
||||
let flows: JSONStore<FlowInfo> = JSONStore::new("./flows");
|
||||
integrity::verify_integrity(&config, &flows, &locations, &itemdb).await;
|
||||
|
||||
for location in &mut *locations {
|
||||
location.1.add(location.0).await;
|
||||
}
|
||||
|
||||
for flow in &mut *flows {
|
||||
flow.1.add(flow.0).await;
|
||||
}
|
||||
|
||||
rocket::build()
|
||||
.mount(
|
||||
"/",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue