From 13bcfb013c4a5ac5ea15c7ebe04f243431165c03 Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 15 Aug 2021 15:13:34 +0300 Subject: Added a WIP file backend Currently unavailable for use and only has basic GET and POST operations implemented. A lot more work is needed to make it truly usable. Locking is implemented using flock() system call on Linux. --- src/database/mod.rs | 67 +++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 30 deletions(-) (limited to 'src/database/mod.rs') diff --git a/src/database/mod.rs b/src/database/mod.rs index 7b144f8..58f0a35 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -7,6 +7,8 @@ mod redis; pub use crate::database::redis::RedisStorage; #[cfg(test)] pub use redis::tests::{get_redis_instance, RedisInstance}; +mod file; +pub use crate::database::file::FileStorage; #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct MicropubChannel { @@ -133,12 +135,6 @@ pub trait Storage: Clone + Send + Sync { /// Note that the `post` object MUST have `post["properties"]["uid"][0]` defined. async fn put_post<'a>(&self, post: &'a serde_json::Value, user: &'a str) -> Result<()>; - /*/// Save a post and add it to the relevant feeds listed in `post["properties"]["channel"]`. - /// - /// Note that the `post` object MUST have `post["properties"]["uid"][0]` defined - /// and `post["properties"]["channel"]` defined, even if it's empty. - async fn put_and_index_post<'a>(&mut self, post: &'a serde_json::Value) -> Result<()>;*/ - /// Modify a post using an update object as defined in the Micropub spec. /// /// Note to implementors: the update operation MUST be atomic OR MUST lock the database @@ -191,6 +187,7 @@ mod tests { use super::redis::tests::get_redis_instance; use super::{MicropubChannel, Storage}; use serde_json::json; + use paste::paste; async fn test_backend_basic_operations(backend: Backend) { let post: serde_json::Value = json!({ @@ -210,7 +207,7 @@ mod tests { .put_post(&post, "https://fireburn.ru/") .await .unwrap(); - if let Ok(Some(returned_post)) = backend.get_post(&key).await { + if let Some(returned_post) = backend.get_post(&key).await.unwrap() { assert!(returned_post.is_object()); assert_eq!( returned_post["type"].as_array().unwrap().len(), @@ -300,30 +297,40 @@ mod tests { "Vika's Hideout" ); } - - #[async_std::test] - async fn test_redis_storage_basic_operations() { - let redis_instance = get_redis_instance().await; - let backend = super::RedisStorage::new(redis_instance.uri().to_string()) - .await - .unwrap(); - test_backend_basic_operations(backend).await; - } - #[async_std::test] - async fn test_redis_storage_channel_list() { - let redis_instance = get_redis_instance().await; - let backend = super::RedisStorage::new(redis_instance.uri().to_string()) - .await - .unwrap(); - test_backend_get_channel_list(backend).await; + macro_rules! redis_test { + ($func_name:expr) => { + paste! { + #[async_std::test] + async fn [] () { + test_logger::ensure_env_logger_initialized(); + let redis_instance = get_redis_instance().await; + let backend = super::RedisStorage::new(redis_instance.uri().to_string()) + .await + .unwrap(); + $func_name(backend).await + } + } + } } - #[async_std::test] - async fn test_redis_settings() { - let redis_instance = get_redis_instance().await; - let backend = super::RedisStorage::new(redis_instance.uri().to_string()) - .await - .unwrap(); - test_backend_settings(backend).await; + macro_rules! file_test { + ($func_name:expr) => { + paste! { + #[async_std::test] + async fn [] () { + test_logger::ensure_env_logger_initialized(); + let tempdir = tempdir::TempDir::new("file").expect("Failed to create tempdir"); + let backend = super::FileStorage::new(tempdir.into_path()).await.unwrap(); + $func_name(backend).await + } + } + } } + + redis_test!(test_backend_basic_operations); + redis_test!(test_backend_get_channel_list); + redis_test!(test_backend_settings); + file_test!(test_backend_basic_operations); + file_test!(test_backend_get_channel_list); + file_test!(test_backend_settings); } -- cgit 1.4.1