edge-schema 0.1.0

Shared schema types for Wasmer Edge.
Documentation
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 = ();
}

#[derive(Serialize, Deserialize, JsonSchema, PartialEq, Eq, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum HttpMethod {
    Get,
    Post,
    Put,
    Delete,
    Patch,
    Head,
    Options,
    Trace,
    Connect,
}

impl HttpMethod {
    pub fn as_str(&self) -> &'static str {
        match self {
            HttpMethod::Get => "GET",
            HttpMethod::Post => "POST",
            HttpMethod::Put => "PUT",
            HttpMethod::Delete => "DELETE",
            HttpMethod::Patch => "PATCH",
            HttpMethod::Head => "HEAD",
            HttpMethod::Options => "OPTIONS",
            HttpMethod::Trace => "TRACE",
            HttpMethod::Connect => "CONNECT",
        }
    }
}

/// 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>;