Skip to content

Commit 6651646

Browse files
committed
trigger-http: Move Server init logic into Server::new
Signed-off-by: Lann Martin <[email protected]>
1 parent 4acac95 commit 6651646

File tree

2 files changed

+36
-45
lines changed

2 files changed

+36
-45
lines changed

crates/trigger-http2/src/lib.rs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod wagi;
1010
mod wasi;
1111

1212
use std::{
13-
collections::HashMap,
1413
error::Error,
1514
net::{Ipv4Addr, SocketAddr, ToSocketAddrs},
1615
path::PathBuf,
@@ -21,7 +20,6 @@ use anyhow::{bail, Context};
2120
use clap::Args;
2221
use serde::Deserialize;
2322
use spin_app::App;
24-
use spin_http::{config::HttpTriggerConfig, routes::Router};
2523
use spin_trigger2::Trigger;
2624
use wasmtime_wasi_http::bindings::wasi::http::types::ErrorCode;
2725

@@ -72,9 +70,6 @@ pub struct HttpTrigger {
7270
/// If the port is set to 0, the actual address will be determined by the OS.
7371
listen_addr: SocketAddr,
7472
tls_config: Option<TlsConfig>,
75-
router: Router,
76-
// Component ID -> component trigger config
77-
component_trigger_configs: HashMap<String, HttpTriggerConfig>,
7873
}
7974

8075
impl Trigger for HttpTrigger {
@@ -109,38 +104,9 @@ impl HttpTrigger {
109104
) -> anyhow::Result<Self> {
110105
Self::validate_app(app)?;
111106

112-
let component_trigger_configs = HashMap::from_iter(
113-
app.trigger_configs::<HttpTriggerConfig>("http")?
114-
.into_iter()
115-
.map(|(_, config)| (config.component.clone(), config)),
116-
);
117-
118-
let component_routes = component_trigger_configs
119-
.iter()
120-
.map(|(component_id, config)| (component_id.as_str(), &config.route));
121-
let (router, duplicate_routes) = Router::build("/", component_routes)?;
122-
if !duplicate_routes.is_empty() {
123-
tracing::error!(
124-
"The following component routes are duplicates and will never be used:"
125-
);
126-
for dup in &duplicate_routes {
127-
tracing::error!(
128-
" {}: {} (duplicate of {})",
129-
dup.replaced_id,
130-
dup.route(),
131-
dup.effective_id,
132-
);
133-
}
134-
}
135-
tracing::trace!(
136-
"Constructed router: {:?}",
137-
router.routes().collect::<Vec<_>>()
138-
);
139107
Ok(Self {
140108
listen_addr,
141109
tls_config,
142-
router,
143-
component_trigger_configs,
144110
})
145111
}
146112

@@ -149,16 +115,8 @@ impl HttpTrigger {
149115
let Self {
150116
listen_addr,
151117
tls_config,
152-
router,
153-
component_trigger_configs,
154118
} = self;
155-
let server = Arc::new(HttpServer::new(
156-
listen_addr,
157-
tls_config,
158-
trigger_app,
159-
router,
160-
component_trigger_configs,
161-
)?);
119+
let server = Arc::new(HttpServer::new(listen_addr, tls_config, trigger_app)?);
162120
Ok(server)
163121
}
164122

crates/trigger-http2/src/server.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,42 @@ impl HttpServer {
5858
listen_addr: SocketAddr,
5959
tls_config: Option<TlsConfig>,
6060
trigger_app: TriggerApp,
61-
router: Router,
62-
component_trigger_configs: HashMap<String, HttpTriggerConfig>,
6361
) -> anyhow::Result<Self> {
62+
// This needs to be a vec before building the router to handle duplicate routes
63+
let component_trigger_configs = Vec::from_iter(
64+
trigger_app
65+
.app()
66+
.trigger_configs::<HttpTriggerConfig>("http")?
67+
.into_iter()
68+
.map(|(_, config)| (config.component.clone(), config)),
69+
);
70+
71+
// Build router
72+
let component_routes = component_trigger_configs
73+
.iter()
74+
.map(|(component_id, config)| (component_id.as_str(), &config.route));
75+
let (router, duplicate_routes) = Router::build("/", component_routes)?;
76+
if !duplicate_routes.is_empty() {
77+
tracing::error!(
78+
"The following component routes are duplicates and will never be used:"
79+
);
80+
for dup in &duplicate_routes {
81+
tracing::error!(
82+
" {}: {} (duplicate of {})",
83+
dup.replaced_id,
84+
dup.route(),
85+
dup.effective_id,
86+
);
87+
}
88+
}
89+
tracing::trace!(
90+
"Constructed router: {:?}",
91+
router.routes().collect::<Vec<_>>()
92+
);
93+
94+
// Now that router is built we can merge duplicate routes by component
95+
let component_trigger_configs = HashMap::from_iter(component_trigger_configs);
96+
6497
let component_handler_types = component_trigger_configs
6598
.keys()
6699
.map(|component_id| {

0 commit comments

Comments
 (0)