2023-06-04 11:17:16 +00:00
|
|
|
pub mod methods;
|
|
|
|
use dotenv::dotenv;
|
2023-05-25 23:02:12 +00:00
|
|
|
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
|
|
|
|
use sqlx::{Error, Pool, Sqlite};
|
2023-06-04 11:17:16 +00:00
|
|
|
use std::env;
|
2023-05-25 23:02:12 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
|
|
|
pub async fn db_init(database: &str) -> Result<Pool<Sqlite>, Error> {
|
|
|
|
let pool = SqlitePoolOptions::new()
|
|
|
|
.max_connections(5)
|
|
|
|
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
/*
|
|
|
|
// https://store.steampowered.com/api/appdetails?appids=1258740
|
|
|
|
sqlx::query(
|
|
|
|
"CREATE TABLE IF NOT EXISTS store_details (
|
|
|
|
id integer primary key,
|
|
|
|
name text not null,
|
|
|
|
item_type text not null,
|
|
|
|
last_timestamp integer not null
|
|
|
|
)",
|
|
|
|
)
|
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
2023-05-25 23:51:36 +00:00
|
|
|
|
2023-05-25 23:02:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// set up indexes?
|
|
|
|
/*
|
|
|
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_estimate ON bus_results (valid_estimate)")
|
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
|
|
|
*/
|
|
|
|
Ok(pool)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_now() -> i64 {
|
|
|
|
if let Ok(x) = SystemTime::now().duration_since(UNIX_EPOCH) {
|
|
|
|
x.as_secs() as i64
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
2023-06-04 11:17:16 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct State {
|
|
|
|
pub db: Pool<Sqlite>,
|
|
|
|
pub config: Config,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Config {
|
|
|
|
ldap_host: String,
|
|
|
|
pub database: String,
|
|
|
|
pub host_port: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_config() -> Config {
|
|
|
|
dotenv().ok();
|
|
|
|
|
|
|
|
// reasonable defaults
|
|
|
|
let mut config = Config {
|
|
|
|
ldap_host: "".to_string(),
|
|
|
|
database: "database.db".to_string(),
|
|
|
|
host_port: "127.0.0.1:8087".to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Ok(x) = env::var("LDAP_HOST") {
|
|
|
|
config.ldap_host = x.trim().to_string();
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("DATABASE") {
|
|
|
|
config.database = x.trim().to_string();
|
|
|
|
}
|
|
|
|
if let Ok(x) = env::var("HOST_PORT") {
|
|
|
|
config.host_port = x.trim().to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|