@@ -2,18 +2,31 @@ use serde::{Deserialize, Serialize};
2
2
use spin_http_routes:: HttpTriggerRouteConfig ;
3
3
4
4
/// Configuration for the HTTP trigger
5
- #[ derive( Clone , Debug , Default , Deserialize , Serialize ) ]
5
+ #[ derive( Clone , Debug , Deserialize , Serialize ) ]
6
6
#[ serde( deny_unknown_fields) ]
7
7
pub struct HttpTriggerConfig {
8
8
/// Component ID to invoke
9
- pub component : String ,
9
+ pub component : Option < String > ,
10
+ /// Static response to send
11
+ pub static_response : Option < StaticResponse > ,
10
12
/// HTTP route the component will be invoked for
11
13
pub route : HttpTriggerRouteConfig ,
12
14
/// The HTTP executor the component requires
13
15
#[ serde( default ) ]
14
16
pub executor : Option < HttpExecutorType > ,
15
17
}
16
18
19
+ impl HttpTriggerConfig {
20
+ pub fn lookup_key ( & self , trigger_id : & str ) -> anyhow:: Result < crate :: routes:: TriggerLookupKey > {
21
+ match ( & self . component , & self . static_response ) {
22
+ ( None , None ) => Err ( anyhow:: anyhow!( "Triggers must specify either component or static_response - {trigger_id} has neither" ) ) ,
23
+ ( Some ( _) , Some ( _) ) => Err ( anyhow:: anyhow!( "Triggers must specify either component or static_response - {trigger_id} has both" ) ) ,
24
+ ( Some ( c) , None ) => Ok ( crate :: routes:: TriggerLookupKey :: Component ( c. to_string ( ) ) ) ,
25
+ ( None , Some ( _) ) => Ok ( crate :: routes:: TriggerLookupKey :: Trigger ( trigger_id. to_string ( ) ) ) ,
26
+ }
27
+ }
28
+ }
29
+
17
30
/// The executor for the HTTP component.
18
31
/// The component can either implement the Spin HTTP interface,
19
32
/// the `wasi-http` interface, or the Wagi CGI interface.
@@ -65,6 +78,32 @@ impl Default for WagiTriggerConfig {
65
78
}
66
79
}
67
80
81
+ /// A static response to be served directly by the host
82
+ /// without instantiating a component.
83
+ #[ derive( Clone , Debug , Deserialize , Serialize ) ]
84
+ pub struct StaticResponse {
85
+ #[ serde( default ) ]
86
+ status_code : Option < u16 > ,
87
+ #[ serde( default ) ]
88
+ headers : indexmap:: IndexMap < String , String > ,
89
+ #[ serde( default ) ]
90
+ body : Option < String > ,
91
+ }
92
+
93
+ impl StaticResponse {
94
+ pub fn status ( & self ) -> u16 {
95
+ self . status_code . unwrap_or ( 200 )
96
+ }
97
+
98
+ pub fn headers ( & self ) -> impl Iterator < Item = ( & String , & String ) > {
99
+ self . headers . iter ( )
100
+ }
101
+
102
+ pub fn body ( & self ) -> Option < & String > {
103
+ self . body . as_ref ( )
104
+ }
105
+ }
106
+
68
107
#[ cfg( test) ]
69
108
mod tests {
70
109
use super :: * ;
0 commit comments