wick_config/config/app_config/triggers/
http.rs1use std::collections::HashMap;
2use std::path::Path;
3
4pub use middleware::{Middleware, MiddlewareBuilder, MiddlewareBuilderError};
5use wick_asset_reference::AssetReference;
6use wick_packet::RuntimeConfig;
7
8pub use self::proxy_router::{ProxyRouterConfig, ProxyRouterConfigBuilder, ProxyRouterConfigBuilderError};
9pub use self::raw_router::{RawRouterConfig, RawRouterConfigBuilder, RawRouterConfigBuilderError};
10pub use self::rest_router::{
11 Contact,
12 Documentation,
13 Info,
14 License,
15 RestRoute,
16 RestRouterConfig,
17 RestRouterConfigBuilder,
18 RestRouterConfigBuilderError,
19 Tools,
20};
21pub use self::static_router::{StaticRouterConfig, StaticRouterConfigBuilder, StaticRouterConfigBuilderError};
22use crate::config::bindings::BoundIdentifier;
23use crate::config::common::template_config::Renderable;
24use crate::config::{Binding, ImportDefinition};
25use crate::error::ManifestError;
26use crate::ExpandImports;
27
28mod middleware;
29mod proxy_router;
30mod raw_router;
31mod rest_router;
32mod static_router;
33
34fn index_to_router_id(trigger_index: usize, index: usize) -> String {
35 format!("trigger_{}_router_{}", trigger_index, index)
36}
37#[derive(
38 Debug, Clone, derive_asset_container::AssetManager, property::Property, serde::Serialize, derive_builder::Builder,
39)]
40#[builder(setter(into))]
41#[property(get(public), set(private), mut(public, suffix = "_mut"))]
42#[asset(asset(AssetReference))]
43#[must_use]
44pub struct HttpTriggerConfig {
45 #[asset(skip)]
46 pub(crate) resource: BoundIdentifier,
47 #[builder(default)]
48 #[serde(skip_serializing_if = "Vec::is_empty")]
49 pub(crate) routers: Vec<HttpRouterConfig>,
50}
51
52#[derive(Debug, Clone, derive_asset_container::AssetManager, serde::Serialize)]
53#[asset(asset(AssetReference))]
54#[must_use]
55#[serde(rename_all = "kebab-case")]
56
57pub enum HttpRouterConfig {
58 RawRouter(RawRouterConfig),
59 RestRouter(RestRouterConfig),
60 StaticRouter(StaticRouterConfig),
61 ProxyRouter(ProxyRouterConfig),
62}
63
64impl Renderable for HttpRouterConfig {
65 fn render_config(
66 &mut self,
67 source: Option<&Path>,
68 root_config: Option<&RuntimeConfig>,
69 env: Option<&HashMap<String, String>>,
70 ) -> Result<(), ManifestError> {
71 match self {
72 HttpRouterConfig::RawRouter(v) => v.render_config(source, root_config, env),
73 HttpRouterConfig::RestRouter(v) => v.render_config(source, root_config, env),
74 HttpRouterConfig::StaticRouter(v) => v.render_config(source, root_config, env),
75 HttpRouterConfig::ProxyRouter(v) => v.render_config(source, root_config, env),
76 }
77 }
78}
79
80impl Renderable for HttpTriggerConfig {
81 fn render_config(
82 &mut self,
83 source: Option<&Path>,
84 root_config: Option<&RuntimeConfig>,
85 env: Option<&HashMap<String, String>>,
86 ) -> Result<(), ManifestError> {
87 self.routers.render_config(source, root_config, env)
88 }
89}
90
91impl ExpandImports for HttpTriggerConfig {
92 type Error = ManifestError;
93 fn expand_imports(
94 &mut self,
95 bindings: &mut Vec<Binding<ImportDefinition>>,
96 trigger_index: usize,
97 ) -> Result<(), Self::Error> {
98 for (router_index, router) in self.routers_mut().iter_mut().enumerate() {
99 match router {
100 HttpRouterConfig::RawRouter(r) => raw_router::process_runtime_config(trigger_index, router_index, r, bindings)?,
101 HttpRouterConfig::StaticRouter(r) => {
102 static_router::process_runtime_config(trigger_index, router_index, r, bindings)?;
103 }
104 HttpRouterConfig::ProxyRouter(r) => {
105 proxy_router::process_runtime_config(trigger_index, router_index, r, bindings)?;
106 }
107 HttpRouterConfig::RestRouter(r) => {
108 rest_router::process_runtime_config(trigger_index, router_index, r, bindings)?;
109 }
110 };
111 }
112
113 Ok(())
114 }
115}
116
117impl HttpRouterConfig {
118 #[must_use]
119 pub const fn kind(&self) -> HttpRouterKind {
120 match self {
121 Self::RawRouter(_) => HttpRouterKind::RawRouter,
122 Self::RestRouter(_) => HttpRouterKind::RestRouter,
123 Self::StaticRouter(_) => HttpRouterKind::StaticRouter,
124 Self::ProxyRouter(_) => HttpRouterKind::ProxyRouter,
125 }
126 }
127
128 #[must_use]
129 pub fn path(&self) -> &str {
130 match self {
131 Self::RawRouter(r) => r.path(),
132 Self::RestRouter(r) => r.path(),
133 Self::StaticRouter(r) => r.path(),
134 Self::ProxyRouter(r) => r.path(),
135 }
136 }
137}
138
139pub trait WickRouter {
140 fn middleware(&self) -> Option<&Middleware>;
141 fn middleware_mut(&mut self) -> Option<&mut Middleware>;
142 fn path(&self) -> &str;
143}
144
145#[derive(Debug, Clone, Copy, serde::Serialize)]
146#[serde(rename_all = "kebab-case")]
147pub enum HttpRouterKind {
148 RawRouter,
149 RestRouter,
150 StaticRouter,
151 ProxyRouter,
152}
153
154impl std::fmt::Display for HttpRouterKind {
155 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
156 match self {
157 Self::RawRouter => write!(f, "raw"),
158 Self::RestRouter => write!(f, "rest"),
159 Self::StaticRouter => write!(f, "static"),
160 Self::ProxyRouter => write!(f, "proxy"),
161 }
162 }
163}