2023-10-10 00:03:04 +02:00
|
|
|
use crate::types::Post;
|
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};
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|