FrostByte/server/src/state.rs

31 lines
691 B
Rust

use crate::types::Post;
use sqlx::Pool;
use sqlx::Sqlite;
use sqlx::{self, sqlite};
use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::Mutex;
use uuid::Uuid;
#[derive(Clone)]
pub struct ServerState {
pub posts: Arc<Mutex<BTreeMap<Uuid, Post>>>,
pub pool: Pool<Sqlite>,
}
impl ServerState {
pub async fn new() -> Self {
let pool = sqlite::SqlitePoolOptions::new()
.max_connections(5)
.connect(":memory:")
.await
.unwrap();
sqlx::migrate!("./migrations").run(&pool).await.unwrap();
Self {
posts: Arc::new(Mutex::new(BTreeMap::new())),
pool: pool,
}
}
}