FrostByte/server/src/state.rs

66 lines
1.6 KiB
Rust
Raw Normal View History

2023-10-21 05:11:42 +02:00
use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::Mutex;
2023-10-10 19:45:18 +02:00
use sqlx::Pool;
2023-10-10 00:03:04 +02:00
use sqlx::Sqlite;
use sqlx::SqlitePool;
2023-10-10 00:03:04 +02:00
use sqlx::{self, sqlite};
2023-10-21 05:11:42 +02:00
#[derive(Clone)]
pub struct CaptchaState {
pub capthca_db: Arc<Mutex<BTreeMap<i32, String>>>,
}
impl CaptchaState {
pub fn new() -> Self {
Self {
capthca_db: Arc::new(Mutex::new(BTreeMap::new())),
}
}
}
2023-10-10 00:03:04 +02:00
#[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(":memory:".to_string());
2023-10-20 20:57:58 +02:00
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();
#[cfg(debug_assertions)]
2023-10-21 00:59:35 +02:00
debug_setup(&pool).await.unwrap();
Self { pool }
2023-10-10 00:03:04 +02:00
}
}
// Inserts a bunch of dummy data into the database
// Mostly useful for debugging new posts, as we need to satisfy foreign key constraints.
#[cfg(debug_assertions)]
2023-10-21 00:59:35 +02:00
async fn debug_setup(pool: &SqlitePool) -> Result<(), sqlx::Error> {
use sqlx::query;
use crate::db::db_new_user;
db_new_user("testuser".to_string(), "testpassword".to_string(), pool).await;
2023-10-21 00:59:35 +02:00
// This requires that the user with id 1 exists in the user table
2023-10-21 00:59:35 +02:00
query!("INSERT INTO posts (user_id, content) VALUES (1, 'Hello world!')",)
.execute(pool)
.await?;
Ok(())
}