1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::{entity::EntityDescriptorConst, WorkloadV2};

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct HttpRouterSpecV1 {
    /// List of domains this router should handle.
    ///
    /// May be empty if the domains are derived from another data source.
    pub domains: Option<Vec<String>>,

    /// Routes to handle.
    pub routes: Vec<HttpRouteV1>,
}

impl EntityDescriptorConst for HttpRouterSpecV1 {
    const NAMESPACE: &'static str = "wasmer.io";
    const NAME: &'static str = "HttpRouter";
    const VERSION: &'static str = "1alpha1";
    const KIND: &'static str = "wasmer.io/HttpRouter.v1alpha1";
    type Spec = Self;
    type State = ();
}

/// An HTTP route of an HTTP router.
#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct HttpRouteV1 {
    /// Optional name for this route.
    /// Only used for debugging.
    pub name: Option<String>,

    /// Ordering priority.
    ///
    /// Will be used to disambiguate between overlapping routes.
    pub priority: Option<i64>,

    /// Matches that must be fulfilled for this route to be selected.
    #[serde(default)]
    pub matches: Vec<HttpRouteMatch>,

    // TODO: add modifiers that can alter request and response.
    // pub request_modifiers: Vec<serde_json::Value>,
    // pub response_modifiers: Vec<serde_json::Value>,
    /// The handler responsible for serving this route.
    pub handler: HttpRouteHandlerV1,
}

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub enum HttpRouteHandlerV1 {
    #[serde(rename = "spawn")]
    Spawn(WorkloadV2),
}

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct HttpRouteMatch {
    pub path: Option<HttpRoutePathMatch>,
    pub methods: Option<Vec<String>>,
    pub headers: Option<Vec<HttpRouteHeaderMatch>>,
}

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub enum HttpRoutePathMatch {
    #[serde(rename = "prefix")]
    Prefix(String),
    #[serde(rename = "exact")]
    Exact(String),
    #[serde(rename = "regex")]
    Regex(String),
}

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub struct HttpRouteHeaderMatch {
    pub header: String,
    pub value: HttpRouteHeaderValueMatch,
}

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
pub enum HttpRouteHeaderValueMatch {
    #[serde(rename = "exact")]
    Exact(String),
    #[serde(rename = "regex")]
    Regex(String),
}

pub type HttpRouterV1 = super::Entity<HttpRouterSpecV1>;