FrostByte/server/src/state.rs

30 lines
739 B
Rust
Raw Normal View History

2023-10-10 19:45:18 +02:00
use sqlx::Pool;
2023-10-10 00:03:04 +02:00
use sqlx::Sqlite;
use sqlx::{self, sqlite};
#[derive(Clone)]
pub struct ServerState {
2023-10-10 00:03:04 +02:00
pub pool: Pool<Sqlite>,
}
impl ServerState {
2023-10-10 00:03:04 +02:00
pub async fn new() -> Self {
2023-10-20 20:57:58 +02:00
// This is almost certainly bad practice for more reasons than I can count
dotenvy::dotenv().ok();
let db_url = dotenvy::var("DATABASE_URL").unwrap_or("sqlite:./db.sqlite".to_string());
2023-10-10 00:03:04 +02:00
let pool = sqlite::SqlitePoolOptions::new()
.max_connections(5)
2023-10-20 20:57:58 +02:00
.connect(&db_url)
2023-10-10 00:03:04 +02:00
.await
.unwrap();
sqlx::migrate!("./migrations").run(&pool).await.unwrap();
Self {
2023-10-20 06:06:55 +02:00
// posts: Arc::new(Mutex::new(BTreeMap::new())),
2023-10-10 00:03:04 +02:00
pool: pool,
}
}
}