use crate::context::Context;
use crate::error::Result;
use async_trait::async_trait;
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub enum InboundAction {
Continue,
Stop,
Capture,
}
#[async_trait]
pub trait InboundMiddleware: Send + Sync + 'static {
async fn process_request(&self, ctx: &mut Context) -> Result<InboundAction>;
fn name(&self) -> &'static str {
"unnamed"
}
fn priority(&self) -> i32 {
0
}
fn should_run(&self, _ctx: &Context) -> bool {
true
}
}
#[async_trait]
pub trait OutboundMiddleware: Send + Sync + 'static {
async fn process_response(&self, ctx: &mut Context) -> Result<()>;
fn priority(&self) -> i32 {
0
}
}
pub trait DualPhaseMiddleware: InboundMiddleware + OutboundMiddleware {
fn is_dual_phase(&self) -> bool {
true
}
}
impl<T> DualPhaseMiddleware for T
where
T: InboundMiddleware + OutboundMiddleware,
{
fn is_dual_phase(&self) -> bool {
true
}
}
pub struct MiddlewareInstance {
pub name: String,
pub priority: i32,
pub inbound: Option<Box<dyn InboundMiddleware>>,
pub outbound: Option<Box<dyn OutboundMiddleware>>,
}
impl MiddlewareInstance {
pub fn inbound<M: InboundMiddleware>(name: &str, middleware: M) -> Self {
let priority = middleware.priority();
Self {
name: name.to_string(),
priority,
inbound: Some(Box::new(middleware)),
outbound: None,
}
}
pub fn outbound<M: OutboundMiddleware>(name: &str, middleware: M) -> Self {
let priority = middleware.priority();
Self {
name: name.to_string(),
priority,
inbound: None,
outbound: Some(Box::new(middleware)),
}
}
pub fn dual<M>(name: &str, middleware: M) -> Self
where
M: InboundMiddleware + OutboundMiddleware + Clone + 'static,
{
let priority = InboundMiddleware::priority(&middleware);
Self {
name: name.to_string(),
priority,
inbound: Some(Box::new(middleware.clone())),
outbound: Some(Box::new(middleware)),
}
}
pub fn has_inbound(&self) -> bool {
self.inbound.is_some()
}
pub fn has_outbound(&self) -> bool {
self.outbound.is_some()
}
}
pub struct MiddlewareBuilder {
name: String,
priority: Option<i32>,
}
impl MiddlewareBuilder {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
priority: None,
}
}
pub fn priority(mut self, priority: i32) -> Self {
self.priority = Some(priority);
self
}
pub fn inbound<M: InboundMiddleware>(self, middleware: M) -> MiddlewareInstance {
let mut instance = MiddlewareInstance::inbound(&self.name, middleware);
if let Some(priority) = self.priority {
instance.priority = priority;
}
instance
}
pub fn outbound<M: OutboundMiddleware>(self, middleware: M) -> MiddlewareInstance {
let mut instance = MiddlewareInstance::outbound(&self.name, middleware);
if let Some(priority) = self.priority {
instance.priority = priority;
}
instance
}
pub fn dual<M>(self, middleware: M) -> MiddlewareInstance
where
M: InboundMiddleware + OutboundMiddleware + Clone + 'static,
{
let mut instance = MiddlewareInstance::dual(&self.name, middleware);
if let Some(priority) = self.priority {
instance.priority = priority;
}
instance
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::context::Context;
use crate::views::ViewEngine;
use std::sync::Arc;
#[derive(Clone)]
struct TestDualMiddleware;
#[async_trait]
impl InboundMiddleware for TestDualMiddleware {
async fn process_request(&self, _ctx: &mut Context) -> Result<InboundAction> {
Ok(InboundAction::Continue)
}
fn priority(&self) -> i32 {
-10
}
}
#[async_trait]
impl OutboundMiddleware for TestDualMiddleware {
async fn process_response(&self, _ctx: &mut Context) -> Result<()> {
Ok(())
}
fn priority(&self) -> i32 {
-10
}
}
#[test]
fn middleware_builder_priority_overrides_default() {
let instance = MiddlewareBuilder::new("test")
.priority(42)
.dual(TestDualMiddleware);
assert_eq!(instance.priority, 42);
}
#[tokio::test]
async fn outbound_instance_uses_outbound_priority() {
#[derive(Clone)]
struct TestOutbound;
#[async_trait]
impl OutboundMiddleware for TestOutbound {
async fn process_response(&self, _ctx: &mut Context) -> Result<()> {
Ok(())
}
fn priority(&self) -> i32 {
7
}
}
let instance = MiddlewareInstance::outbound("outbound", TestOutbound);
assert_eq!(instance.priority, 7);
let views = Arc::new(ViewEngine::new());
let mut ctx = Context::new(crate::http::Request::default(), views);
instance
.outbound
.as_ref()
.unwrap()
.process_response(&mut ctx)
.await
.unwrap();
}
}