Initial commit

This commit is contained in:
Lennart
2023-09-04 13:20:13 +02:00
commit ccb09f40b4
26 changed files with 10064 additions and 0 deletions

17
src/config.rs Normal file
View File

@@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct JsonCalendarStoreConfig {
pub db_path: String,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "backend", rename_all = "snake_case")]
pub enum CalendarStoreConfig {
Json(JsonCalendarStoreConfig),
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub calendar_store: CalendarStoreConfig,
}

63
src/main.rs Normal file
View File

@@ -0,0 +1,63 @@
use std::fs;
use std::sync::Arc;
use crate::config::Config;
use actix_web::middleware::{Logger, NormalizePath};
use actix_web::{web, App, HttpServer};
use anyhow::Result;
use clap::Parser;
use config::{CalendarStoreConfig, JsonCalendarStoreConfig};
use rustical_api::configure_api;
use rustical_dav::{configure_dav, configure_well_known};
use rustical_store::calendar::JsonCalendarStore;
use tokio::sync::RwLock;
mod config;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(short, long, env)]
config_file: String,
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));
let args = Args::parse();
let config: Config = toml::from_str(&fs::read_to_string(&args.config_file)?)?;
// TODO: Clean this jank up as soon more configuration options appear
let db_path = match config.calendar_store {
CalendarStoreConfig::Json(JsonCalendarStoreConfig { db_path }) => db_path,
};
let cal_store = Arc::new(RwLock::new(
if let Ok(json) = fs::read_to_string(&db_path) {
serde_json::from_str::<JsonCalendarStore>(&json)?
} else {
JsonCalendarStore::new(db_path.to_string())
},
));
HttpServer::new(move || {
let cal_store = cal_store.clone();
App::new()
.wrap(Logger::new("[%s] %r"))
.wrap(NormalizePath::trim())
.service(
web::scope("/dav").configure(|cfg| configure_dav(cfg, cal_store.clone().into())),
)
.service(
web::scope("/.well-known")
.configure(|cfg| configure_well_known(cfg, "/dav".to_string())),
)
.service(
web::scope("/api").configure(|cfg| configure_api(cfg, cal_store.clone().into())),
)
})
.bind(("0.0.0.0", 4000))?
.run()
.await?;
Ok(())
}