edge_schema/schema/
http_router.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use super::{entity::EntityDescriptorConst, WorkloadV2};
5
6#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
7pub struct HttpRouterSpecV1 {
8    /// List of domains this router should handle.
9    ///
10    /// May be empty if the domains are derived from another data source.
11    pub domains: Option<Vec<String>>,
12
13    /// Routes to handle.
14    pub routes: Vec<HttpRouteV1>,
15}
16
17impl EntityDescriptorConst for HttpRouterSpecV1 {
18    const NAMESPACE: &'static str = "wasmer.io";
19    const NAME: &'static str = "HttpRouter";
20    const VERSION: &'static str = "1alpha1";
21    const KIND: &'static str = "wasmer.io/HttpRouter.v1alpha1";
22    type Spec = Self;
23    type State = ();
24}
25
26#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
27#[serde(rename_all = "lowercase")]
28pub enum HttpMethod {
29    Get,
30    Post,
31    Put,
32    Delete,
33    Patch,
34    Head,
35    Options,
36    Trace,
37    Connect,
38}
39
40impl HttpMethod {
41    pub fn as_str(&self) -> &'static str {
42        match self {
43            HttpMethod::Get => "GET",
44            HttpMethod::Post => "POST",
45            HttpMethod::Put => "PUT",
46            HttpMethod::Delete => "DELETE",
47            HttpMethod::Patch => "PATCH",
48            HttpMethod::Head => "HEAD",
49            HttpMethod::Options => "OPTIONS",
50            HttpMethod::Trace => "TRACE",
51            HttpMethod::Connect => "CONNECT",
52        }
53    }
54}
55
56/// An HTTP route of an HTTP router.
57#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
58pub struct HttpRouteV1 {
59    /// Optional name for this route.
60    /// Only used for debugging.
61    pub name: Option<String>,
62
63    /// Ordering priority.
64    ///
65    /// Will be used to disambiguate between overlapping routes.
66    pub priority: Option<i64>,
67
68    /// Matches that must be fulfilled for this route to be selected.
69    #[serde(default)]
70    pub matches: Vec<HttpRouteMatch>,
71
72    // TODO: add modifiers that can alter request and response.
73    // pub request_modifiers: Vec<serde_json::Value>,
74    // pub response_modifiers: Vec<serde_json::Value>,
75    /// The handler responsible for serving this route.
76    pub handler: HttpRouteHandlerV1,
77}
78
79#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
80pub enum HttpRouteHandlerV1 {
81    #[serde(rename = "spawn")]
82    Spawn(WorkloadV2),
83}
84
85#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
86pub struct HttpRouteMatch {
87    pub path: Option<HttpRoutePathMatch>,
88    pub methods: Option<Vec<String>>,
89    pub headers: Option<Vec<HttpRouteHeaderMatch>>,
90}
91
92#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
93pub enum HttpRoutePathMatch {
94    #[serde(rename = "prefix")]
95    Prefix(String),
96    #[serde(rename = "exact")]
97    Exact(String),
98    #[serde(rename = "regex")]
99    Regex(String),
100}
101
102#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
103pub struct HttpRouteHeaderMatch {
104    pub header: String,
105    pub value: HttpRouteHeaderValueMatch,
106}
107
108#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
109pub enum HttpRouteHeaderValueMatch {
110    #[serde(rename = "exact")]
111    Exact(String),
112    #[serde(rename = "regex")]
113    Regex(String),
114}
115
116pub type HttpRouterV1 = super::Entity<HttpRouterSpecV1>;