FrostByte/server/src/state.rs
2023-10-10 00:03:04 +02:00

31 lines
696 B
Rust

use crate::types::Post;
use sqlx::Sqlite;
use sqlx::{self, sqlite};
use sqlx::{AnyPool, Pool};
use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::Mutex;
use uuid::Uuid;
#[derive(Clone)]
pub struct AppState {
pub posts: Arc<Mutex<BTreeMap<Uuid, Post>>>,
pub pool: Pool<Sqlite>,
}
impl AppState {
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,
}
}
}