mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 15:52:38 +00:00
Add WIP sqlite store
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1644,6 +1644,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"clap",
|
||||
"env_logger",
|
||||
"rustical_api",
|
||||
@@ -1651,6 +1652,7 @@ dependencies = [
|
||||
"rustical_caldav",
|
||||
"rustical_store",
|
||||
"serde",
|
||||
"sqlx",
|
||||
"tokio",
|
||||
"toml",
|
||||
"tracing",
|
||||
|
||||
12
Cargo.toml
12
Cargo.toml
@@ -22,3 +22,15 @@ actix-web = "4.4.0"
|
||||
anyhow = { version = "1.0.75", features = ["backtrace"] }
|
||||
toml = "0.7.6"
|
||||
clap = { version = "4.4.2", features = ["derive", "env"] }
|
||||
sqlx = { version = "0.7.1", features = [
|
||||
"sqlx-sqlite",
|
||||
"sqlx-postgres",
|
||||
"uuid",
|
||||
"time",
|
||||
"chrono",
|
||||
"postgres",
|
||||
"sqlite",
|
||||
"runtime-tokio",
|
||||
"migrate",
|
||||
] }
|
||||
async-trait = "0.1.73"
|
||||
|
||||
@@ -19,6 +19,7 @@ sqlx = { version = "0.7.1", features = [
|
||||
"postgres",
|
||||
"sqlite",
|
||||
"runtime-tokio",
|
||||
"migrate",
|
||||
] }
|
||||
tokio = { version = "1.32.0", features = ["sync", "full"] }
|
||||
toml = "0.7.6"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod calendar;
|
||||
pub mod event;
|
||||
pub mod sqlite_store;
|
||||
pub mod timestamps;
|
||||
pub mod toml_store;
|
||||
|
||||
54
crates/store/src/sqlite_store.rs
Normal file
54
crates/store/src/sqlite_store.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::calendar::{Calendar, CalendarStore};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SqliteCalendarStore {
|
||||
db: Arc<SqlitePool>,
|
||||
}
|
||||
|
||||
impl SqliteCalendarStore {
|
||||
pub fn new(db: Arc<SqlitePool>) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CalendarStore for SqliteCalendarStore {
|
||||
async fn get_calendar(&self, id: &str) -> Result<Calendar> {
|
||||
let a = sqlx::query_as!(
|
||||
Calendar,
|
||||
"SELECT id, name, owner, description, color, timezone FROM calendars WHERE id = ?",
|
||||
id
|
||||
);
|
||||
Err(anyhow!("ok wow"))
|
||||
}
|
||||
|
||||
async fn get_calendars(&self, _owner: &str) -> Result<Vec<Calendar>> {
|
||||
Err(anyhow!("ok wow"))
|
||||
}
|
||||
|
||||
async fn insert_calendar(&mut self, _cid: String, _calendar: Calendar) -> Result<()> {
|
||||
Err(anyhow!("ok wow"))
|
||||
}
|
||||
|
||||
async fn get_events(&self, _cid: &str) -> Result<Vec<crate::event::Event>> {
|
||||
Err(anyhow!("ok wow"))
|
||||
}
|
||||
|
||||
async fn get_event(&self, _cid: &str, _uid: &str) -> Result<crate::event::Event> {
|
||||
Err(anyhow!("ok wow"))
|
||||
}
|
||||
|
||||
async fn upsert_event(&mut self, _cid: String, _uid: String, _ics: String) -> Result<()> {
|
||||
Err(anyhow!("ok wow"))
|
||||
}
|
||||
|
||||
async fn delete_event(&mut self, _cid: &str, _uid: &str) -> Result<()> {
|
||||
Err(anyhow!("ok wow"))
|
||||
}
|
||||
}
|
||||
@@ -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)]
|
||||
|
||||
17
src/main.rs
17
src/main.rs
@@ -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());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user