47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
use sqlx::Pool;
|
|
use sqlx::Sqlite;
|
|
use sqlx::SqlitePool;
|
|
use sqlx::{self, sqlite};
|
|
|
|
#[derive(Clone)]
|
|
pub struct ServerState {
|
|
pub pool: Pool<Sqlite>,
|
|
}
|
|
|
|
impl ServerState {
|
|
pub async fn new() -> Self {
|
|
// 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());
|
|
|
|
let pool = sqlite::SqlitePoolOptions::new()
|
|
.max_connections(5)
|
|
.connect(&db_url)
|
|
.await
|
|
.unwrap();
|
|
|
|
sqlx::migrate!("./migrations").run(&pool).await.unwrap();
|
|
|
|
#[cfg(debug_assertions)]
|
|
debug_setup(&pool).await.unwrap();
|
|
|
|
Self { pool }
|
|
}
|
|
}
|
|
|
|
// 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)]
|
|
async fn debug_setup(pool: &SqlitePool) -> Result<(), sqlx::Error> {
|
|
use sqlx::query;
|
|
|
|
query!("INSERT INTO users (username, password) VALUES ('testuser', 'testpassword')",)
|
|
.execute(pool)
|
|
.await?;
|
|
|
|
query!("INSERT INTO posts (user_id, content) VALUES (1, 'Hello world!')",)
|
|
.execute(pool)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|