-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error if more than one handler is found in a component
It's ambiguous which to run in that case so present an error instead of picking an arbitrary one. Signed-off-by: Alex Crichton <[email protected]>
- Loading branch information
1 parent
012dda8
commit bd5bfa8
Showing
1 changed file
with
24 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
use std::{net::SocketAddr, str, str::FromStr}; | ||
|
||
use crate::{Body, HttpExecutor, HttpInstance, HttpTrigger, Store}; | ||
use anyhow::bail; | ||
use anyhow::{anyhow, Context, Result}; | ||
use futures::TryFutureExt; | ||
use http::{HeaderName, HeaderValue}; | ||
|
@@ -331,21 +330,36 @@ const WASI_HTTP_EXPORT_0_2_0: &str = "wasi:http/[email protected]"; | |
impl HandlerType { | ||
/// Determine the handler type from the exports of a component | ||
pub fn from_component(ty: &ComponentType) -> Result<HandlerType> { | ||
let mut handler_ty = None; | ||
|
||
let mut set = |ty: HandlerType| { | ||
if handler_ty.is_none() { | ||
handler_ty = Some(ty); | ||
Ok(()) | ||
} else { | ||
Err(anyhow!( | ||
"component exports multiple different handlers but \ | ||
it's expected to export only one" | ||
)) | ||
} | ||
}; | ||
for (name, _) in ty.exports() { | ||
match name { | ||
WASI_HTTP_EXPORT_2023_10_18 => return Ok(HandlerType::Wasi2023_10_18), | ||
WASI_HTTP_EXPORT_2023_11_10 => return Ok(HandlerType::Wasi2023_11_10), | ||
WASI_HTTP_EXPORT_0_2_0 => return Ok(HandlerType::Wasi0_2), | ||
"fermyon:spin/inbound-http" => return Ok(HandlerType::Spin), | ||
WASI_HTTP_EXPORT_2023_10_18 => set(HandlerType::Wasi2023_10_18)?, | ||
WASI_HTTP_EXPORT_2023_11_10 => set(HandlerType::Wasi2023_11_10)?, | ||
WASI_HTTP_EXPORT_0_2_0 => set(HandlerType::Wasi0_2)?, | ||
"fermyon:spin/inbound-http" => set(HandlerType::Spin)?, | ||
_ => {} | ||
} | ||
} | ||
|
||
bail!( | ||
"Expected component to either export `{WASI_HTTP_EXPORT_2023_10_18}`, \ | ||
`{WASI_HTTP_EXPORT_2023_11_10}`, `{WASI_HTTP_EXPORT_0_2_0}`, \ | ||
or `fermyon:spin/inbound-http` but it exported none of those" | ||
) | ||
handler_ty.ok_or_else(|| { | ||
anyhow!( | ||
"Expected component to either export `{WASI_HTTP_EXPORT_2023_10_18}`, \ | ||
`{WASI_HTTP_EXPORT_2023_11_10}`, `{WASI_HTTP_EXPORT_0_2_0}`, \ | ||
or `fermyon:spin/inbound-http` but it exported none of those" | ||
) | ||
}) | ||
} | ||
} | ||
|
||
|