Compare commits

...

3 Commits

Author SHA1 Message Date
Nutomic ac715c7f74
Merge ef76b48505 into b152be7951 2024-05-06 08:37:58 -05:00
Felix Ableitner ef76b48505 reduce deps 2024-05-06 15:34:36 +02:00
Felix Ableitner caf9d20dea add after hook 2024-05-06 14:13:06 +02:00
3 changed files with 30 additions and 36 deletions

18
Cargo.lock generated
View File

@ -2163,7 +2163,6 @@ dependencies = [
"toml 0.8.12",
"tracing",
"tracing-subscriber",
"ureq",
"url",
"uuid",
"wasmtime",
@ -6865,23 +6864,6 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "ureq"
version = "2.9.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d11a831e3c0b56e438a28308e7c810799e3c118417f342d30ecec080105395cd"
dependencies = [
"base64 0.22.1",
"flate2",
"log",
"once_cell",
"rustls 0.22.4",
"rustls-pki-types",
"rustls-webpki 0.102.3",
"url",
"webpki-roots 0.26.1",
]
[[package]]
name = "url"
version = "2.5.0"

View File

@ -207,8 +207,10 @@ clap = { workspace = true }
serde.workspace = true
actix-web-prom = "0.7.0"
actix-http = "3.6.0"
extism = "1.2.0"
extism-convert = "1.2.0"
extism = { version = "1.2.0", features = [
"register-filesystem",
], default-features = false }
extism-convert = { version = "1.2.0", default-features = false }
[dev-dependencies]
pretty_assertions = { workspace = true }

View File

@ -1,4 +1,4 @@
use actix_http::h1::Payload;
use actix_http::{body::BoxBody, h1::Payload};
use actix_web::{
body::MessageBody,
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
@ -22,13 +22,12 @@ impl PluginMiddleware {
PluginMiddleware {}
}
}
impl<S, B> Transform<S, ServiceRequest> for PluginMiddleware
impl<S> Transform<S, ServiceRequest> for PluginMiddleware
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
S::Future: 'static,
B: MessageBody + 'static,
{
type Response = ServiceResponse<B>;
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type Transform = SessionService<S>;
type InitError = ();
@ -45,13 +44,12 @@ pub struct SessionService<S> {
service: Rc<S>,
}
impl<S, B> Service<ServiceRequest> for SessionService<S>
impl<S> Service<ServiceRequest> for SessionService<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S: Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
@ -61,27 +59,39 @@ where
let svc = self.service.clone();
Box::pin(async move {
let method = service_req.method();
let method = service_req.method().clone();
let path = service_req.path().replace("/api/v3/", "").replace("/", "_");
// TODO: naming can be a bit silly, `POST /api/v3/post` becomes `api_before_post_post`
let plugin_hook = format!("api_before_{method}_{path}").to_lowercase();
let before_plugin_hook = format!("api_before_{method}_{path}").to_lowercase();
info!("Calling plugin hook {}", &plugin_hook);
info!("Calling plugin hook {}", &before_plugin_hook);
if let Some(mut plugins) = load_plugins()? {
if plugins.function_exists(&plugin_hook) {
if plugins.function_exists(&before_plugin_hook) {
let payload = service_req.extract::<Bytes>().await?;
let mut json: Value = serde_json::from_slice(&payload.to_vec())?;
call_plugin(plugins, &plugin_hook, &mut json)?;
call_plugin(plugins, &before_plugin_hook, &mut json)?;
let (_, mut new_payload) = Payload::create(true);
new_payload.unread_data(Bytes::from(serde_json::to_vec_pretty(&json)?));
new_payload.unread_data(Bytes::from(serde_json::to_vec(&json)?));
service_req.set_payload(new_payload.into());
}
}
let res = svc.call(service_req).await?;
let mut res = svc.call(service_req).await?;
// TODO: add after hook
let after_plugin_hook = format!("api_after_{method}_{path}").to_lowercase();
info!("Calling plugin hook {}", &after_plugin_hook);
if let Some(mut plugins) = load_plugins()? {
if plugins.function_exists(&before_plugin_hook) {
res = res.map_body(|_, body| {
let mut json: Value =
serde_json::from_slice(&body.try_into_bytes().unwrap().to_vec()).unwrap();
call_plugin(plugins, &after_plugin_hook, &mut json).unwrap();
BoxBody::new(Bytes::from(serde_json::to_vec(&json).unwrap()))
});
}
}
Ok(res)
})