Add WIP sqlite store

This commit is contained in:
Lennart
2023-10-08 14:48:51 +02:00
parent f6cf5bd645
commit 4f2e92c953
7 changed files with 88 additions and 5 deletions

View File

@@ -6,10 +6,16 @@ pub struct TomlCalendarStoreConfig {
pub db_path: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SqliteCalendarStoreConfig {
pub db_url: String,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "backend", rename_all = "snake_case")]
pub enum CalendarStoreConfig {
Toml(TomlCalendarStoreConfig),
Sqlite(SqliteCalendarStoreConfig),
}
#[derive(Debug, Deserialize, Serialize)]

View File

@@ -3,11 +3,14 @@ use actix_web::middleware::{Logger, NormalizePath};
use actix_web::{web, App, HttpServer};
use anyhow::Result;
use clap::Parser;
use config::{CalendarStoreConfig, TomlCalendarStoreConfig};
use config::{CalendarStoreConfig, SqliteCalendarStoreConfig, TomlCalendarStoreConfig};
use rustical_api::configure_api;
use rustical_auth::AuthProvider;
use rustical_caldav::{configure_dav, configure_well_known};
use rustical_store::calendar::CalendarStore;
use rustical_store::sqlite_store::SqliteCalendarStore;
use rustical_store::toml_store::TomlCalendarStore;
use sqlx::SqlitePool;
use std::fs;
use std::sync::Arc;
use tokio::sync::RwLock;
@@ -28,14 +31,18 @@ async fn main() -> Result<()> {
let args = Args::parse();
let config: Config = toml::from_str(&fs::read_to_string(&args.config_file)?)?;
let cal_store = Arc::new(RwLock::new(match &config.calendar_store {
let cal_store: Arc<RwLock<dyn CalendarStore>> = match &config.calendar_store {
CalendarStoreConfig::Toml(TomlCalendarStoreConfig { db_path }) => {
match fs::read_to_string(db_path) {
Arc::new(RwLock::new(match fs::read_to_string(db_path) {
Ok(content) => toml::from_str::<TomlCalendarStore>(&content).unwrap(),
Err(_) => TomlCalendarStore::new(db_path.to_string()),
}
}))
}
}));
CalendarStoreConfig::Sqlite(SqliteCalendarStoreConfig { db_url }) => {
let db = SqlitePool::connect(db_url).await?;
Arc::new(RwLock::new(SqliteCalendarStore::new(Arc::new(db))))
}
};
let auth: Arc<AuthProvider> = Arc::new(config.auth.into());