Skip to main content

ordinary_config/app/middleware/
mod.rs

1// Copyright (C) 2026 The Ordinary Authors.
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5use serde::{Deserialize, Serialize};
6use std::collections::BTreeSet;
7
8#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
9#[cfg_attr(feature = "docs", derive(schemars::JsonSchema))]
10#[derive(Deserialize, Serialize, Debug, Clone)]
11pub enum MiddlewareMechanism {
12    Request { endpoint: String },
13}
14
15#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
16#[cfg_attr(feature = "docs", derive(schemars::JsonSchema))]
17#[derive(Deserialize, Serialize, Debug, Clone)]
18pub enum MiddlewareValidationRule {
19    /// if any of the rules included pass it is valid.
20    #[cfg_attr(feature = "utoipa", schema(no_recursion))]
21    Any(Vec<Box<MiddlewareValidationRule>>),
22    /// only if all the rules pass is it valid.
23    #[cfg_attr(feature = "utoipa", schema(no_recursion))]
24    All(Vec<Box<MiddlewareValidationRule>>),
25    /// if the rule is not true then it is valid.
26    #[cfg_attr(feature = "utoipa", schema(no_recursion))]
27    Not(Box<MiddlewareValidationRule>),
28
29    /// status code that will allow the
30    /// request to proceed.
31    StatusCode(u16),
32}
33
34#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
35#[cfg_attr(feature = "docs", derive(schemars::JsonSchema))]
36#[derive(Deserialize, Serialize, Debug, Clone, PartialOrd, PartialEq, Ord, Eq)]
37pub enum MiddlewareValidationComponent {
38    /// pass headers through to the validator
39    Headers,
40    /// append the path to the endpoint's path
41    Path,
42    /// append query string params from the request
43    Query,
44}
45
46#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
47#[cfg_attr(feature = "docs", derive(schemars::JsonSchema))]
48#[derive(Deserialize, Serialize, Debug, Clone)]
49pub enum MiddlewareOperation {
50    Validate {
51        rule: MiddlewareValidationRule,
52        components: BTreeSet<MiddlewareValidationComponent>,
53    },
54}
55
56/// configuration structure for HTTP middleware
57#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
58#[cfg_attr(feature = "docs", derive(schemars::JsonSchema))]
59#[derive(Deserialize, Serialize, Debug, Clone)]
60pub struct MiddlewareConfig {
61    /// name by which this middleware will be referenced
62    /// on other resources
63    pub name: String,
64
65    /// the operation the middleware will perform
66    pub operation: MiddlewareOperation,
67
68    /// how the middleware operation will be performed
69    pub mechanism: MiddlewareMechanism,
70}