From 96f2b1821e5cfe90be81baac8a6b4a2f9780d477 Mon Sep 17 00:00:00 2001 From: Gusto Bacvinka Date: Tue, 17 Jan 2023 00:04:53 +0200 Subject: [PATCH] Simplify handlers for post and other data methods --- nomos-services/http/src/backends/axum.rs | 31 ++++++++++-------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/nomos-services/http/src/backends/axum.rs b/nomos-services/http/src/backends/axum.rs index 5a78ab1b..43e86876 100644 --- a/nomos-services/http/src/backends/axum.rs +++ b/nomos-services/http/src/backends/axum.rs @@ -138,27 +138,22 @@ impl AxumBackend { } fn add_data_route(&self, method: HttpMethod, path: &str, req_stream: Sender) { - let handler = match method { - HttpMethod::POST => post( - |Query(query): Query>, payload: Option| async move { - handle_req(req_stream, query, payload).await - }, - ), - HttpMethod::PUT => put( - |Query(query): Query>, payload: Option| async move { - handle_req(req_stream, query, payload).await - }, - ), - HttpMethod::PATCH => patch( - |Query(query): Query>, payload: Option| async move { - handle_req(req_stream, query, payload).await - }, - ), - _ => unimplemented!(), + let handler_fn = |Query(query): Query>, payload: Option| async move { + handle_req(req_stream, query, payload).await }; + let handlers = { + let mut handlers = HashMap::new(); + handlers.insert(HttpMethod::POST, post(handler_fn.clone())); + handlers.insert(HttpMethod::PUT, put(handler_fn.clone())); + handlers.insert(HttpMethod::PATCH, patch(handler_fn)); + handlers + }; + + let handler = handlers.get(&method).unwrap_or_else(|| unimplemented!()); + let mut router = self.router.lock(); - *router = router.clone().route(path, handler) + *router = router.clone().route(path, handler.clone()) } }