yggdrasil/src/database.rs

63 lines
1.9 KiB
Rust

/*
* Yggdrasil: Minecraft authentication server
* Copyright (C) 2023 0xf8.dev@proton.me
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::env;
use std::path::PathBuf;
use anyhow::Result;
use ftlog::{debug, error, info, log, trace, warn};
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::SqlitePool;
use crate::config::Config;
pub struct Database {
db: SqlitePool,
}
impl Database {
pub async fn init(config: &Config) -> Result<Self> {
let pool = SqlitePoolOptions::new().max_connections(5).connect("").await?;
Ok(Self {
db: pool
})
}
async fn open(config: &Config) -> Result<SqlitePool> {
Ok(SqlitePoolOptions::new().max_connections(5).connect(&env::var("DATABASE_URL")?).await?)
}
async fn new(config: &Config) -> Result<SqlitePool> {
let pool = Self::open(&config).await?;
pool.
let mut conn = pool.acquire().await?;
sqlx::query!(r#"
"#).execute(&mut conn).await?;
pool.prepare("").await?;
unimplemented!()
}
async fn ensure_db(config: &Config) -> Result<SqlitePool> {
let db_path = PathBuf::from(&env::var("DATABASE_URL")?);
match db_path.try_exists()? {
true => Self::open(&config).await,
false => Self::new(&config).await
}
}
}