@@ -1181,8 +1181,38 @@ fn httpd_ulp_endpoints(
1181
1181
fn httpd (
1182
1182
mutex : Arc < ( Mutex < Option < u32 > > , Condvar ) > ,
1183
1183
) -> Result < esp_idf_svc:: http:: server:: EspHttpServer > {
1184
- use embedded_svc:: http:: server:: { Method , Request , Response } ;
1184
+ use embedded_svc:: http:: server:: {
1185
+ handler, Connection , FnHandler , Handler , HandlerResult , Method , Middleware , Request ,
1186
+ Response ,
1187
+ } ;
1185
1188
use embedded_svc:: io:: Write ;
1189
+ use esp_idf_svc:: http:: server:: EspHttpConnection ;
1190
+
1191
+ struct SampleMiddleware { }
1192
+
1193
+ impl < C > Middleware < C > for SampleMiddleware
1194
+ where
1195
+ C : Connection ,
1196
+ {
1197
+ fn handle < ' a , H > ( & ' a self , connection : C , handler : & ' a H ) -> HandlerResult
1198
+ where
1199
+ H : Handler < C > ,
1200
+ {
1201
+ info ! ( "Middleware called" ) ;
1202
+
1203
+ handler. handle ( connection)
1204
+ }
1205
+ }
1206
+
1207
+ // Necessary, until I figure out a more decent solution that does not
1208
+ // refer to `EspHttpConnection`, but to the Connection trait instead
1209
+ // (if at all possible)
1210
+ fn fix_hrtb < F > ( f : F ) -> F
1211
+ where
1212
+ F : for <' a , ' b > Fn ( Request < & ' a mut EspHttpConnection < ' b > > ) -> HandlerResult ,
1213
+ {
1214
+ f
1215
+ }
1186
1216
1187
1217
let mut server = esp_idf_svc:: http:: server:: EspHttpServer :: new ( & Default :: default ( ) ) ?;
1188
1218
@@ -1202,9 +1232,19 @@ fn httpd(
1202
1232
1203
1233
Ok ( ( ) )
1204
1234
} ) ?
1205
- . fn_handler ( "/panic" , Method :: Get , |_req | {
1235
+ . fn_handler ( "/panic" , Method :: Get , |req | {
1206
1236
panic ! ( "User requested a panic!" )
1207
- } ) ?;
1237
+ } ) ?
1238
+ . handler (
1239
+ "/middleware" ,
1240
+ Method :: Get ,
1241
+ SampleMiddleware { } . compose ( handler ( fix_hrtb ( |req| {
1242
+ req. into_ok_response ( ) ?
1243
+ . write_all ( "Middleware handler called" . as_bytes ( ) ) ?;
1244
+
1245
+ Ok ( ( ) )
1246
+ } ) ) ) ,
1247
+ ) ?;
1208
1248
1209
1249
#[ cfg( esp32s2) ]
1210
1250
httpd_ulp_endpoints ( & mut server, mutex) ?;
0 commit comments