Building plugin api

This commit is contained in:
eraden 2023-06-01 22:02:47 +02:00
parent 313982a720
commit aaf45f3954
40 changed files with 402 additions and 136 deletions

198
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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<Self> {
async fn new(config: PluginConfig) -> CResult<Self> {
let config: MemoryConfig = config.config::<MemoryConfig>().map_err(|e| {
error!("Failed to parse memory cache config: {e}");
Error::InvalidConfig

View File

@ -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<Self> {
async fn new(config: PluginConfig) -> CResult<Self> {
RedisCacheAdapter::connect(config.config().map_err(|e| {
error!("Failed to parse redis cache config: {e}");
Error::InvalidConfig

View File

@ -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" }

View File

@ -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<T> = Result<T, Error>;
pub struct Config(pub String);
impl Config {
pub fn config<S: serde::de::DeserializeOwned>(self) -> Result<S, toml::de::Error> {
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<Self>;
async fn new(config: PluginConfig) -> CResult<Self>;
async fn read<T>(&mut self, key: &str) -> CResult<Option<T>>
where

View File

@ -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'] }

View File

@ -875,3 +875,13 @@ pub fn init_tracing(_service_name: &str) {
.try_init()
.unwrap();
}
pub struct PluginConfig(pub String);
impl PluginConfig {
pub fn config<S: serde::de::DeserializeOwned>(
&self,
) -> std::result::Result<S, toml::de::Error> {
toml::from_str(&self.0)
}
}

View File

@ -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" }

View File

@ -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<T> = Result<T, EBError>;
pub struct Config(pub String);
impl Config {
pub fn config<S: serde::de::DeserializeOwned>(self) -> Result<S, toml::de::Error> {
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<Item = Message>,
Sender: MessageSend,
{
async fn connect(config: Config) -> Result<(Stream, Sender), ()>;
async fn connect(config: PluginConfig) -> Result<(Stream, Sender), ()>;
}
/// For client-server purpose only

View File

@ -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" }

View File

@ -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<MessageStream, MessageSender> 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)

View File

@ -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" }

View File

@ -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<T> = Result<T, Error>;
#[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<Self>;
async fn store(&mut self, file: impl futures::AsyncRead + Unpin + Send) -> SResult<Url>;
async fn erase(&mut self, file_name: Url) -> SResult<usize>;
}

View File

@ -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" }

View File

@ -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<RwLock<Config>>,
}
#[async_trait]
impl FileStorage for FileStorageLocal {
fn name() -> &'static str {
"file-storage-local"
}
async fn new(config: &PluginConfig) -> SResult<Self> {
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<Url> {
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<usize> {
tokio::fs::remove_file(format!("{url}"))
.await
.map_err(|e| Error::DeleteFile(url, e))
.map(|_| 1)
}
}

View File

@ -0,0 +1,6 @@
[package]
name = "file-storage-s3"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -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);
}
}

View File

@ -1,5 +1,5 @@
[package]
name = "payment_adapter"
name = "payment-adapter"
version = "0.1.0"
edition = "2021"

View File

@ -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`).

View File

@ -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<T> = Result<T, PError>;
pub struct Config(String);
impl Config {
pub fn config<S: serde::de::DeserializeOwned>(self) -> Result<S, toml::de::Error> {
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<T: PaymentSessionData + Any>(
) -> Option<&mut T> {
<dyn Any>::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<dyn Plugin>>,
}
pub fn find_plugin<'plugins, P: Plugin>(
plugins: &'plugins Plugins,
name: &str,
) -> Option<&'plugins P> {
let plugin = plugins.plugins.get(name)?;
<dyn Any>::downcast_ref(plugin)
}
impl Plugins {
pub fn insert_plugin<P>(&mut self, plugin: P)
where
P: Plugin,
{
self.plugins.insert(plugin.name(), Box::new(plugin));
}
}

View File

@ -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" }

View File

@ -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" }

View File

@ -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"] }

View File

@ -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" }

View File

@ -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<dyn Plugin>>,
}
pub fn find_plugin<'plugins, P: Plugin>(
plugins: &'plugins Plugins,
name: &str,
) -> Option<&'plugins P> {
let plugin = plugins.plugins.get(name)?;
<dyn Any>::downcast_ref(plugin)
}
impl Plugins {
pub fn insert_plugin<P>(&mut self, plugin: P)
where
P: Plugin,
{
self.plugins.insert(plugin.name(), Box::new(plugin));
}
}