diff --git a/Cargo.lock b/Cargo.lock index 921badc..e797820 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,7 +12,7 @@ dependencies = [ "config", "dotenv", "fake", - "futures", + "futures 0.3.28", "gumdrop", "json", "model", @@ -640,6 +640,16 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "bytes" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" +dependencies = [ + "byteorder", + "iovec", +] + [[package]] name = "bytes" version = "0.5.6" @@ -666,9 +676,10 @@ name = "cache-adapter" version = "0.1.0" dependencies = [ "async-trait", + "config", "serde", "thiserror", - "toml 0.7.3", + "toml 0.7.4", ] [[package]] @@ -694,7 +705,7 @@ dependencies = [ "cache-adapter", "chrono", "deadpool", - "futures", + "futures 0.3.28", "redis", "redis-async-pool", "serde", @@ -776,7 +787,7 @@ dependencies = [ "config", "dotenv", "fake", - "futures", + "futures 0.3.28", "model", "rumqttc", "serde", @@ -818,9 +829,9 @@ dependencies = [ "bincode", "bytes 1.4.0", "config", - "futures", + "futures 0.3.28", "model", - "payment_adapter", + "payment-adapter", "rumqttc", "serde", "strum", @@ -828,7 +839,7 @@ dependencies = [ "thiserror", "tokio 1.28.2", "tracing", - "uuid 1.3.2", + "uuid 1.3.3", "whatlang", ] @@ -922,7 +933,7 @@ dependencies = [ "serde", "serde_json", "thiserror", - "toml 0.7.3", + "toml 0.7.4", "tracing-subscriber", "tracing-timing", ] @@ -1578,11 +1589,12 @@ name = "event-bus-adapter" version = "0.1.0" dependencies = [ "bincode", + "config", "event-bus-messages", - "futures", + "futures 0.3.28", "serde", "thiserror", - "toml 0.7.3", + "toml 0.7.4", ] [[package]] @@ -1600,11 +1612,12 @@ name = "event-bus-redis" version = "0.1.0" dependencies = [ "event-bus-adapter", - "futures", + "futures 0.3.28", "futures-util", "redis-async", "serde", "thiserror", + "toml 0.7.4", "tracing", ] @@ -1625,7 +1638,7 @@ dependencies = [ "http", "rand 0.8.5", "unidecode", - "uuid 1.3.2", + "uuid 1.3.3", ] [[package]] @@ -1666,6 +1679,35 @@ dependencies = [ "log", ] +[[package]] +name = "file-storage-adapter" +version = "0.1.0" +dependencies = [ + "async-trait", + "config", + "futures 0.3.28", + "futures-util", + "thiserror", + "tracing", +] + +[[package]] +name = "file-storage-local" +version = "0.1.0" +dependencies = [ + "async-trait", + "file-storage-adapter", + "futures 0.3.28", + "serde", + "tokio 1.28.2", + "tracing", + "uuid 1.3.3", +] + +[[package]] +name = "file-storage-s3" +version = "0.1.0" + [[package]] name = "filetime" version = "0.2.21" @@ -1823,6 +1865,12 @@ dependencies = [ "tracing", ] +[[package]] +name = "futures" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" + [[package]] name = "futures" version = "0.3.28" @@ -1926,6 +1974,7 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ + "futures 0.1.31", "futures-channel", "futures-core", "futures-io", @@ -1936,6 +1985,7 @@ dependencies = [ "pin-project-lite 0.2.9", "pin-utils", "slab", + "tokio-io", ] [[package]] @@ -3004,7 +3054,7 @@ dependencies = [ "sqlx-core", "thiserror", "tracing", - "uuid 1.3.2", + "uuid 1.3.3", "validator", ] @@ -3243,7 +3293,7 @@ dependencies = [ "thiserror", "tokio 1.28.2", "tracing", - "uuid 1.3.2", + "uuid 1.3.3", ] [[package]] @@ -3335,7 +3385,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" [[package]] -name = "pay_u_adapter" +name = "payment-adapter" +version = "0.1.0" +dependencies = [ + "actix-web", + "async-trait", + "chrono", + "config", + "futures 0.1.31", + "model", + "serde", + "thiserror", + "toml 0.7.4", + "tracing", + "traitcast", + "uuid 1.3.3", +] + +[[package]] +name = "payment-pay-u" version = "0.1.0" dependencies = [ "async-trait", @@ -3343,7 +3411,7 @@ dependencies = [ "chrono", "common_macros", "fulfillment_adapter", - "payment_adapter", + "payment-adapter", "serde", "serde_json", "thiserror", @@ -3351,21 +3419,19 @@ dependencies = [ ] [[package]] -name = "payment_adapter" +name = "payment-stripe" version = "0.1.0" dependencies = [ "actix-web", + "async-stripe", "async-trait", - "chrono", - "config", - "futures", - "model", + "derive_more", + "fulfillment_adapter", + "payment-adapter", "serde", "thiserror", - "toml 0.7.3", + "tokio 1.28.2", "tracing", - "traitcast", - "uuid 1.3.2", ] [[package]] @@ -3381,7 +3447,7 @@ dependencies = [ "gumdrop", "llvmenv", "model", - "payment_adapter", + "payment-adapter", "reqwest 0.11.17", "rumqttc", "serde", @@ -3392,7 +3458,7 @@ dependencies = [ "thiserror", "tokio 1.28.2", "tracing", - "uuid 1.3.2", + "uuid 1.3.3", "wapc", "wapc-codec", "wapc-pool", @@ -3494,6 +3560,17 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +[[package]] +name = "plugin-api" +version = "0.1.0" +dependencies = [ + "actix-web", + "async-trait", + "config", + "thiserror", + "tracing", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -3846,7 +3923,7 @@ dependencies = [ "async-trait", "bytes 1.4.0", "combine", - "futures", + "futures 0.3.28", "futures-util", "itoa 1.0.6", "percent-encoding", @@ -4115,7 +4192,7 @@ checksum = "04483567c64bb8a9d64364a0a9437215a056a2b886140fd66e62a12394cf5998" dependencies = [ "bytes 1.4.0", "flume", - "futures", + "futures 0.3.28", "log", "rustls-native-certs", "rustls-pemfile", @@ -4239,7 +4316,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ed36cdb20de66d89a17ea04b8883fc7a386f2cf877aaedca5005583ce4876ff" dependencies = [ "crossbeam-channel", - "futures", + "futures 0.3.28", "futures-channel", "futures-executor", "num_cpus", @@ -4300,7 +4377,7 @@ dependencies = [ "derive_more", "dotenv", "fake", - "futures", + "futures 0.3.28", "model", "parking_lot 0.12.1", "rumqttc", @@ -4311,7 +4388,7 @@ dependencies = [ "thiserror", "tokio 1.28.2", "tracing", - "uuid 1.3.2", + "uuid 1.3.3", "whatlang", ] @@ -4428,9 +4505,9 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0efd8caf556a6cebd3b285caf480045fcc1ac04f6bd786b09a6f11af30c4fcf4" +checksum = "93107647184f6027e3b7dcb2e11034cf95ffa1e3a682c67951963ac69c1c007d" dependencies = [ "serde", ] @@ -4701,7 +4778,7 @@ dependencies = [ "time 0.3.20", "tokio-stream", "url", - "uuid 1.3.2", + "uuid 1.3.3", "webpki-roots", "whoami", ] @@ -4760,7 +4837,7 @@ dependencies = [ "derive_more", "dotenv", "fakeit", - "futures", + "futures 0.3.28", "insta", "model", "rumqttc", @@ -4772,7 +4849,7 @@ dependencies = [ "thiserror", "tokio 1.28.2", "tracing", - "uuid 1.3.2", + "uuid 1.3.3", ] [[package]] @@ -4785,22 +4862,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "stripe_adapter" -version = "0.1.0" -dependencies = [ - "actix-web", - "async-stripe", - "async-trait", - "derive_more", - "fulfillment_adapter", - "payment_adapter", - "serde", - "thiserror", - "tokio 1.28.2", - "tracing", -] - [[package]] name = "strsim" version = "0.8.0" @@ -4928,7 +4989,7 @@ checksum = "6f41bce44d290df0598ae4b9cd6ea7f58f651fd3aa4af1b26060c4fa32b08af7" dependencies = [ "anyhow", "fnv", - "futures", + "futures 0.3.28", "humantime", "opentelemetry", "pin-project", @@ -5104,7 +5165,7 @@ dependencies = [ "derive_more", "dotenv", "fake", - "futures", + "futures 0.3.28", "futures-util", "gumdrop", "hmac", @@ -5122,7 +5183,7 @@ dependencies = [ "thiserror", "tokio 1.28.2", "tracing", - "uuid 1.3.2", + "uuid 1.3.3", ] [[package]] @@ -5162,6 +5223,17 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "tokio-io" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" +dependencies = [ + "bytes 0.4.12", + "futures 0.1.31", + "log", +] + [[package]] name = "tokio-macros" version = "2.1.0" @@ -5271,9 +5343,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b403acf6f2bb0859c93c7f0d967cb4a75a7ac552100f9322faf64dc047669b21" +checksum = "d6135d499e69981f9ff0ef2167955a5333c35e36f6937d382974566b3d5b94ec" dependencies = [ "serde", "serde_spanned", @@ -5283,18 +5355,18 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ab8ed2edee10b50132aed5f331333428b011c99402b5a534154ed15746f9622" +checksum = "5a76a9312f5ba4c2dec6b9161fdf25d87ad8a09256ccea5a556fef03c706a10f" dependencies = [ "serde", ] [[package]] name = "toml_edit" -version = "0.19.8" +version = "0.19.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239410c8609e8125456927e6707163a3b1fdb40561e4b803bc041f466ccfdc13" +checksum = "2380d56e8670370eee6566b0bfd4265f65b3f432e8c6d85623f728d4fa31f739" dependencies = [ "indexmap", "serde", @@ -5562,9 +5634,9 @@ dependencies = [ [[package]] name = "uuid" -version = "1.3.2" +version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dad5567ad0cf5b760e5665964bec1b47dfd077ba8a2544b513f3556d3d239a2" +checksum = "345444e32442451b267fc254ae85a209c64be56d2890e601a0c37ff0c3c5ecd2" dependencies = [ "atomic", "getrandom 0.2.9", diff --git a/Cargo.toml b/Cargo.toml index 87c5407..dbc9b98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ members = [ "crates/token_manager", # "crates/fs_manager", "crates/lang_provider", - "crates/payment_adapter", "crates/fulfillment_adapter", # "crates/payment_adapter_pay_u", # artifacts @@ -28,8 +27,12 @@ members = [ # vendor # "vendor/t_pay", # "vendor/pay_u", - "crates/pay_u_adapter", - "crates/stripe_adapter", + # PLUGINS + "crates/plugin-api", + # PAYMENT + "crates/payment-adapter", + "crates/payment-pay-u", + "crates/payment-stripe", # EVENT BUS "crates/event-bus-messages", "crates/event-bus-adapter", @@ -38,6 +41,10 @@ members = [ "crates/cache-adapter", "crates/cache-adapter-redis", "crates/cache-adapter-embedded-memory", + # FILE STORAGE + "crates/file-storage-adapter", + "crates/file-storage-local", + "crates/file-storage-s3", ] exclude = [ "crates/web" diff --git a/crates/cache-adapter-embedded-memory/src/lib.rs b/crates/cache-adapter-embedded-memory/src/lib.rs index 6c1c5cf..f11d383 100644 --- a/crates/cache-adapter-embedded-memory/src/lib.rs +++ b/crates/cache-adapter-embedded-memory/src/lib.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock}; use std::time::Duration; use async_trait::async_trait; -use cache_adapter::{CResult, CacheAdapter, Config, Error, InvalidatePattern}; +use cache_adapter::{CResult, CacheAdapter, Error, InvalidatePattern, PluginConfig}; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use tracing::error; @@ -26,7 +26,7 @@ pub struct EmbeddedMemoryCacheAdapter { #[async_trait] impl CacheAdapter for EmbeddedMemoryCacheAdapter { - async fn new(config: Config) -> CResult { + async fn new(config: PluginConfig) -> CResult { let config: MemoryConfig = config.config::().map_err(|e| { error!("Failed to parse memory cache config: {e}"); Error::InvalidConfig diff --git a/crates/cache-adapter-redis/src/lib.rs b/crates/cache-adapter-redis/src/lib.rs index 9de7aaf..536b1b2 100644 --- a/crates/cache-adapter-redis/src/lib.rs +++ b/crates/cache-adapter-redis/src/lib.rs @@ -1,7 +1,7 @@ use std::time::Duration; use async_trait::async_trait; -use cache_adapter::{CResult, CacheAdapter, Config, Error, InvalidatePattern}; +use cache_adapter::{CResult, CacheAdapter, Error, InvalidatePattern, PluginConfig}; use redis::AsyncCommands; use redis_async_pool::*; use serde::de::DeserializeOwned; @@ -52,7 +52,7 @@ impl RedisCacheAdapter { #[async_trait] impl CacheAdapter for RedisCacheAdapter { - async fn new(config: Config) -> CResult { + async fn new(config: PluginConfig) -> CResult { RedisCacheAdapter::connect(config.config().map_err(|e| { error!("Failed to parse redis cache config: {e}"); Error::InvalidConfig diff --git a/crates/cache-adapter/Cargo.toml b/crates/cache-adapter/Cargo.toml index 8146feb..576aa2d 100644 --- a/crates/cache-adapter/Cargo.toml +++ b/crates/cache-adapter/Cargo.toml @@ -8,3 +8,4 @@ async-trait = { version = "0.1.68" } serde = { version = "1.0.163", feeatures = ['derive'] } thiserror = { version = "1.0.40" } toml = { version = "0.7.3" } +config = { path = "../config" } diff --git a/crates/cache-adapter/src/lib.rs b/crates/cache-adapter/src/lib.rs index 831d19e..9ba21cc 100644 --- a/crates/cache-adapter/src/lib.rs +++ b/crates/cache-adapter/src/lib.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use async_trait::async_trait; +pub use config::PluginConfig; #[derive(Debug, thiserror::Error)] pub enum Error { @@ -16,14 +17,6 @@ pub enum Error { pub type CResult = Result; -pub struct Config(pub String); - -impl Config { - pub fn config(self) -> Result { - toml::from_str(&self.0) - } -} - #[derive(Debug)] pub enum InvalidatePattern<'s> { StartsWith(Cow<'s, str>), @@ -34,7 +27,7 @@ pub enum InvalidatePattern<'s> { #[async_trait] pub trait CacheAdapter: Sized { - async fn new(config: Config) -> CResult; + async fn new(config: PluginConfig) -> CResult; async fn read(&mut self, key: &str) -> CResult> where diff --git a/crates/channels/Cargo.toml b/crates/channels/Cargo.toml index 1bab4af..60443f7 100644 --- a/crates/channels/Cargo.toml +++ b/crates/channels/Cargo.toml @@ -10,7 +10,7 @@ emails = [] search = [] stocks = [] orders = [] -payments = ['payment_adapter'] +payments = ['payment-adapter'] tokens = [] default = ['accounts', 'carts', 'emails', 'search', 'stocks', 'orders', 'payments', 'tokens'] @@ -20,7 +20,7 @@ bytes = { version = "1" } config = { path = "../config" } futures = { version = "0" } model = { path = "../model" } -payment_adapter = { path = "../payment_adapter", optional = true } +payment-adapter = { path = "../payment-adapter", optional = true } rumqttc = { version = "*" } serde = { version = "*", features = ['derive'] } strum = { version = "0", features = ['strum_macros', 'default', 'derive'] } diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index a7ba889..9c80f89 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -875,3 +875,13 @@ pub fn init_tracing(_service_name: &str) { .try_init() .unwrap(); } + +pub struct PluginConfig(pub String); + +impl PluginConfig { + pub fn config( + &self, + ) -> std::result::Result { + toml::from_str(&self.0) + } +} diff --git a/crates/event-bus-adapter/Cargo.toml b/crates/event-bus-adapter/Cargo.toml index 9d57f53..9be2108 100644 --- a/crates/event-bus-adapter/Cargo.toml +++ b/crates/event-bus-adapter/Cargo.toml @@ -10,3 +10,4 @@ thiserror = { version = "1.0.40" } event-bus-messages = { path = "../event-bus-messages" } futures = { version = "0.3.28" } toml = { version = "0.7.3" } +config = { path = "../config" } diff --git a/crates/event-bus-adapter/src/lib.rs b/crates/event-bus-adapter/src/lib.rs index cb326ab..f74fa7f 100644 --- a/crates/event-bus-adapter/src/lib.rs +++ b/crates/event-bus-adapter/src/lib.rs @@ -1,5 +1,6 @@ #![feature(async_fn_in_trait)] +pub use config::PluginConfig; pub use event_bus_messages::*; #[derive(Debug, thiserror::Error)] @@ -14,14 +15,6 @@ pub enum EBError { pub type EBResult = Result; -pub struct Config(pub String); - -impl Config { - pub fn config(self) -> Result { - toml::from_str(&self.0) - } -} - pub trait MessageSend { async fn send(&mut self, topic: Topic, msg: Msg) -> EBResult<()>; } @@ -31,7 +24,7 @@ where Stream: futures::stream::Stream, Sender: MessageSend, { - async fn connect(config: Config) -> Result<(Stream, Sender), ()>; + async fn connect(config: PluginConfig) -> Result<(Stream, Sender), ()>; } /// For client-server purpose only diff --git a/crates/event-bus-redis/Cargo.toml b/crates/event-bus-redis/Cargo.toml index 7e38153..0f0c0a9 100644 --- a/crates/event-bus-redis/Cargo.toml +++ b/crates/event-bus-redis/Cargo.toml @@ -11,3 +11,4 @@ futures-util = { version = "0.3.28" } futures = { version = "0.3.28" } tracing = { version = "0" } serde = { version = "1.0.162" } +toml = { version = "0.7.4" } diff --git a/crates/event-bus-redis/src/lib.rs b/crates/event-bus-redis/src/lib.rs index 6045902..11c98b5 100644 --- a/crates/event-bus-redis/src/lib.rs +++ b/crates/event-bus-redis/src/lib.rs @@ -3,7 +3,9 @@ use std::pin::Pin; use std::task::{Context, Poll}; -use event_bus_adapter::{Config, EBError, EBResult, EventBus, Message, MessageSend, Msg, Topic}; +use event_bus_adapter::{ + EBError, EBResult, EventBus, Message, MessageSend, Msg, PluginConfig, Topic, +}; use futures_util::stream::{SplitSink, SplitStream}; use futures_util::{SinkExt, StreamExt}; use redis_async::client::connect::RespConnection; @@ -83,7 +85,7 @@ pub struct RedisEventBusConfig { pub struct RedisEventBus; impl EventBus for RedisEventBus { - async fn connect(config: Config) -> Result<(MessageStream, MessageSender), ()> { + async fn connect(config: PluginConfig) -> Result<(MessageStream, MessageSender), ()> { let RedisEventBusConfig { host, port } = config.config().expect("Invalid redis bus config"); let client = redis_async::client::connect(&host, port) diff --git a/crates/file-storage-adapter/Cargo.toml b/crates/file-storage-adapter/Cargo.toml new file mode 100644 index 0000000..66c4055 --- /dev/null +++ b/crates/file-storage-adapter/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "file-storage-adapter" +version = "0.1.0" +edition = "2021" + +[dependencies] +thiserror = { version = "1.0.40" } +async-trait = { version = "0.1.68" } +tracing = { version = "0.1.37" } +config = { path = "../config" } +futures = { version = "0.3.28", features = ["async-await", 'io-compat'] } +futures-util = { version = "0.3.28" } diff --git a/crates/file-storage-adapter/src/lib.rs b/crates/file-storage-adapter/src/lib.rs new file mode 100644 index 0000000..7608e7b --- /dev/null +++ b/crates/file-storage-adapter/src/lib.rs @@ -0,0 +1,44 @@ +use std::fmt::{Display, Formatter, Write}; +use std::path::PathBuf; + +use async_trait::async_trait; +pub use config::PluginConfig; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("Invalid config format")] + ConfigFormat, + #[error("Failed to create storage root directory {0:?}: {1:?}")] + CreateRootDir(String, std::io::Error), + #[error("Failed to create file {0:?}: {1:?}")] + OutputFile(PathBuf, std::io::Error), + #[error("Failed to delete file {0}: {1:?}")] + DeleteFile(Url, std::io::Error), +} + +pub type SResult = Result; + +#[derive(Debug)] +pub struct Url { + pub base_url: String, + pub file_name: String, +} + +impl Display for Url { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.base_url)?; + f.write_char('/')?; + f.write_str(&self.file_name) + } +} + +#[async_trait] +pub trait FileStorage: Sized { + fn name() -> &'static str; + + async fn new(config: &PluginConfig) -> SResult; + + async fn store(&mut self, file: impl futures::AsyncRead + Unpin + Send) -> SResult; + + async fn erase(&mut self, file_name: Url) -> SResult; +} diff --git a/crates/file-storage-local/Cargo.toml b/crates/file-storage-local/Cargo.toml new file mode 100644 index 0000000..9bf384e --- /dev/null +++ b/crates/file-storage-local/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "file-storage-local" +version = "0.1.0" +edition = "2021" + +[dependencies] +file-storage-adapter = { path = "../file-storage-adapter" } +tokio = { version = "1.28.2", features = ['full'] } +futures = { version = "0.3.28" } +async-trait = { version = "0.1.68" } +serde = { version = "1.0.163", features = ['derive'] } +uuid = { version = "1.3.3", features = ['v4'] } +tracing = { version = "0.1" } diff --git a/crates/file-storage-local/src/lib.rs b/crates/file-storage-local/src/lib.rs new file mode 100644 index 0000000..273e813 --- /dev/null +++ b/crates/file-storage-local/src/lib.rs @@ -0,0 +1,74 @@ +use std::path::Path; +use std::sync::{Arc, RwLock}; + +use async_trait::async_trait; +use file_storage_adapter::*; +use futures::{AsyncRead, AsyncReadExt}; +use serde::{Deserialize, Serialize}; +use tokio::io::AsyncWriteExt; +use tracing::error; +use uuid::Uuid; + +#[derive(Serialize, Deserialize)] +pub struct Config { + base_url: String, + root: String, +} + +pub struct FileStorageLocal { + config: Arc>, +} + +#[async_trait] +impl FileStorage for FileStorageLocal { + fn name() -> &'static str { + "file-storage-local" + } + + async fn new(config: &PluginConfig) -> SResult { + Ok(Self { + config: Arc::new(RwLock::new(config.config().map_err(|e| { + error!("Invalid config for local file storage: {e}"); + Error::ConfigFormat + })?)), + }) + } + + async fn store(&mut self, mut input_stream: impl AsyncRead + Unpin + Send) -> SResult { + let (root, base_url) = { + let config = self.config.read().unwrap(); + let root = config.root.clone(); + let base_url = config.base_url.clone(); + (root, base_url) + }; + let name = Uuid::new_v4(); + let dir = Path::new(&root).to_path_buf(); + if !dir.exists() { + tokio::fs::create_dir_all(&dir) + .await + .map_err(|e| Error::CreateRootDir(root.clone(), e))?; + } + + let file_path = dir.join(format!("{name}")); + let mut file = tokio::fs::File::create(file_path.clone()) + .await + .map_err(|e| Error::OutputFile(file_path.clone(), e))?; + let mut buffer = [0; 4086]; + while let Ok(len) = input_stream.read(&mut buffer).await { + file.write(&buffer[..len]) + .await + .map_err(|e| Error::OutputFile(file_path.clone(), e))?; + } + Ok(Url { + base_url, + file_name: format!("{name}"), + }) + } + + async fn erase(&mut self, url: Url) -> SResult { + tokio::fs::remove_file(format!("{url}")) + .await + .map_err(|e| Error::DeleteFile(url, e)) + .map(|_| 1) + } +} diff --git a/crates/file-storage-s3/Cargo.toml b/crates/file-storage-s3/Cargo.toml new file mode 100644 index 0000000..76fdd10 --- /dev/null +++ b/crates/file-storage-s3/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "file-storage-s3" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/crates/file-storage-s3/src/lib.rs b/crates/file-storage-s3/src/lib.rs new file mode 100644 index 0000000..7d12d9a --- /dev/null +++ b/crates/file-storage-s3/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/crates/payment_adapter/Cargo.toml b/crates/payment-adapter/Cargo.toml similarity index 95% rename from crates/payment_adapter/Cargo.toml rename to crates/payment-adapter/Cargo.toml index c95435b..e2a6c9e 100644 --- a/crates/payment_adapter/Cargo.toml +++ b/crates/payment-adapter/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "payment_adapter" +name = "payment-adapter" version = "0.1.0" edition = "2021" diff --git a/crates/payment_adapter/Readme.adoc b/crates/payment-adapter/Readme.adoc similarity index 98% rename from crates/payment_adapter/Readme.adoc rename to crates/payment-adapter/Readme.adoc index daff1a7..027cd42 100644 --- a/crates/payment_adapter/Readme.adoc +++ b/crates/payment-adapter/Readme.adoc @@ -1,6 +1,6 @@ = Payment Adapter Implementation -== Extention Lifetime +== Extension Lifetime Payment Adapter extension is loaded as WASI file and initialize as a module. Then to store it in runtime `name` function is called to load extension unique `name` (`String`). diff --git a/crates/payment_adapter/src/lib.rs b/crates/payment-adapter/src/lib.rs similarity index 88% rename from crates/payment_adapter/src/lib.rs rename to crates/payment-adapter/src/lib.rs index ec1ff74..3f34e57 100644 --- a/crates/payment_adapter/src/lib.rs +++ b/crates/payment-adapter/src/lib.rs @@ -3,7 +3,7 @@ use std::any::Any; use std::collections::HashMap; -pub use config::PaymentProviderConfig; +pub use config::{PaymentProviderConfig, PluginConfig}; pub use model::v3::*; pub use uuid; use uuid::Uuid; @@ -138,17 +138,9 @@ pub enum PError { pub type PResult = Result; -pub struct Config(String); - -impl Config { - pub fn config(self) -> Result { - toml::from_str(&self.0) - } -} - #[async_trait::async_trait] pub trait PaymentAdapter { - async fn new(config: Config) -> Self; + async fn new(config: PluginConfig) -> Self; fn identifier(&self) -> &'static str; @@ -238,30 +230,3 @@ pub fn session_mut_ref( ) -> Option<&mut T> { ::downcast_mut(session) } - -pub trait Plugin: Any { - fn name(&self) -> &'static str; - - fn mount(&self, config: &mut actix_web::web::ServiceConfig); -} - -pub struct Plugins { - plugins: HashMap<&'static str, Box>, -} - -pub fn find_plugin<'plugins, P: Plugin>( - plugins: &'plugins Plugins, - name: &str, -) -> Option<&'plugins P> { - let plugin = plugins.plugins.get(name)?; - ::downcast_ref(plugin) -} - -impl Plugins { - pub fn insert_plugin

(&mut self, plugin: P) - where - P: Plugin, - { - self.plugins.insert(plugin.name(), Box::new(plugin)); - } -} diff --git a/crates/pay_u_adapter/Cargo.lock b/crates/payment-pay-u/Cargo.lock similarity index 100% rename from crates/pay_u_adapter/Cargo.lock rename to crates/payment-pay-u/Cargo.lock diff --git a/crates/pay_u_adapter/Cargo.toml b/crates/payment-pay-u/Cargo.toml similarity index 87% rename from crates/pay_u_adapter/Cargo.toml rename to crates/payment-pay-u/Cargo.toml index 57d1b39..49a2f25 100644 --- a/crates/pay_u_adapter/Cargo.toml +++ b/crates/payment-pay-u/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "pay_u_adapter" +name = "payment-pay-u" version = "0.1.0" edition = "2021" @@ -14,7 +14,7 @@ bincode = { version = "1" } tracing = { version = "0" } chrono = { version = "0", features = ['alloc', 'wasmbind'] } common_macros = { version = "0" } -payment_adapter = { path = "../payment_adapter" } +payment-adapter = { path = "../payment-adapter" } fulfillment_adapter = { path = "../fulfillment_adapter" } serde = { version = "1", features = ['derive'] } serde_json = { version = "1" } diff --git a/crates/pay_u_adapter/src/credit.rs b/crates/payment-pay-u/src/credit.rs similarity index 100% rename from crates/pay_u_adapter/src/credit.rs rename to crates/payment-pay-u/src/credit.rs diff --git a/crates/pay_u_adapter/src/deserialize.rs b/crates/payment-pay-u/src/deserialize.rs similarity index 100% rename from crates/pay_u_adapter/src/deserialize.rs rename to crates/payment-pay-u/src/deserialize.rs diff --git a/crates/pay_u_adapter/src/lib.rs b/crates/payment-pay-u/src/lib.rs similarity index 100% rename from crates/pay_u_adapter/src/lib.rs rename to crates/payment-pay-u/src/lib.rs diff --git a/crates/pay_u_adapter/src/model.rs b/crates/payment-pay-u/src/model.rs similarity index 100% rename from crates/pay_u_adapter/src/model.rs rename to crates/payment-pay-u/src/model.rs diff --git a/crates/pay_u_adapter/src/notify.rs b/crates/payment-pay-u/src/notify.rs similarity index 100% rename from crates/pay_u_adapter/src/notify.rs rename to crates/payment-pay-u/src/notify.rs diff --git a/crates/pay_u_adapter/src/req.rs b/crates/payment-pay-u/src/req.rs similarity index 100% rename from crates/pay_u_adapter/src/req.rs rename to crates/payment-pay-u/src/req.rs diff --git a/crates/pay_u_adapter/src/res.rs b/crates/payment-pay-u/src/res.rs similarity index 100% rename from crates/pay_u_adapter/src/res.rs rename to crates/payment-pay-u/src/res.rs diff --git a/crates/pay_u_adapter/src/serialize.rs b/crates/payment-pay-u/src/serialize.rs similarity index 100% rename from crates/pay_u_adapter/src/serialize.rs rename to crates/payment-pay-u/src/serialize.rs diff --git a/crates/stripe_adapter/Cargo.toml b/crates/payment-stripe/Cargo.toml similarity index 91% rename from crates/stripe_adapter/Cargo.toml rename to crates/payment-stripe/Cargo.toml index 627ebf5..ba5d667 100644 --- a/crates/stripe_adapter/Cargo.toml +++ b/crates/payment-stripe/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "stripe_adapter" +name = "payment-stripe" version = "0.1.0" edition = "2021" @@ -25,7 +25,7 @@ crate-type = ['dylib'] #rustflags = ["-C", "prefer-dynamic", "-C", "rpath"] [dependencies] -payment_adapter = { path = "../payment_adapter" } +payment-adapter = { path = "../payment-adapter" } fulfillment_adapter = { path = "../fulfillment_adapter" } tokio = { version = "1.27.0" } tracing = { version = "0.1.37" } diff --git a/crates/stripe_adapter/src/lib.rs b/crates/payment-stripe/src/lib.rs similarity index 100% rename from crates/stripe_adapter/src/lib.rs rename to crates/payment-stripe/src/lib.rs diff --git a/crates/stripe_adapter/src/lib/adapters.rs b/crates/payment-stripe/src/lib/adapters.rs similarity index 100% rename from crates/stripe_adapter/src/lib/adapters.rs rename to crates/payment-stripe/src/lib/adapters.rs diff --git a/crates/stripe_adapter/src/lib/web.rs b/crates/payment-stripe/src/lib/web.rs similarity index 100% rename from crates/stripe_adapter/src/lib/web.rs rename to crates/payment-stripe/src/lib/web.rs diff --git a/crates/stripe_adapter/src/przelewy_24.rs b/crates/payment-stripe/src/przelewy_24.rs similarity index 100% rename from crates/stripe_adapter/src/przelewy_24.rs rename to crates/payment-stripe/src/przelewy_24.rs diff --git a/crates/stripe_adapter/src/routes.rs b/crates/payment-stripe/src/routes.rs similarity index 100% rename from crates/stripe_adapter/src/routes.rs rename to crates/payment-stripe/src/routes.rs diff --git a/crates/payment_manager/Cargo.toml b/crates/payment_manager/Cargo.toml index f948c73..49812d2 100644 --- a/crates/payment_manager/Cargo.toml +++ b/crates/payment_manager/Cargo.toml @@ -16,7 +16,7 @@ db-utils = { path = "../db-utils" } gumdrop = { version = "0", features = [] } llvmenv = { version = "0" } model = { path = "../model", features = ["db"] } -payment_adapter = { path = "../payment_adapter" } +payment-adapter = { path = "../payment-adapter" } reqwest = { version = "0", features = ["default", "json", "blocking"] } rumqttc = { version = "*" } serde = { version = "1", features = ["derive"] } diff --git a/crates/plugin-api/Cargo.toml b/crates/plugin-api/Cargo.toml new file mode 100644 index 0000000..32a5154 --- /dev/null +++ b/crates/plugin-api/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "plugin-api" +version = "0.1.0" +edition = "2021" + +[dependencies] +config = { path = "../config", default-features = false, features = [] } +thiserror = { version = "1" } +tracing = { version = "0" } +async-trait = { version = "0.1.68" } +actix-web = { version = "4.3.1" } diff --git a/crates/plugin-api/src/lib.rs b/crates/plugin-api/src/lib.rs new file mode 100644 index 0000000..0e27f09 --- /dev/null +++ b/crates/plugin-api/src/lib.rs @@ -0,0 +1,47 @@ +use std::any::Any; +use std::collections::HashMap; + +use async_trait::async_trait; + +#[derive(Copy, Clone, Debug)] +pub enum PluginType { + Payment, + Cache, + FileStorage, +} + +#[async_trait] +pub trait Plugin: Any { + fn plugin_type() -> PluginType + where + Self: Sized; + + fn name(&self) -> &'static str; + + fn mount(&self, config: &mut actix_web::web::ServiceConfig); + + async fn initialize(app_config: &config::AppConfig) + where + Self: Sized; +} + +pub struct Plugins { + plugins: HashMap<&'static str, Box>, +} + +pub fn find_plugin<'plugins, P: Plugin>( + plugins: &'plugins Plugins, + name: &str, +) -> Option<&'plugins P> { + let plugin = plugins.plugins.get(name)?; + ::downcast_ref(plugin) +} + +impl Plugins { + pub fn insert_plugin

(&mut self, plugin: P) + where + P: Plugin, + { + self.plugins.insert(plugin.name(), Box::new(plugin)); + } +}