From 750fdd9c1f7a7b6e1591f5b0d768cc039ce75c38 Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Tue, 28 Mar 2023 06:55:51 +0000 Subject: [PATCH] fix: global lock middleware func signature --- cmd/service/api.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/cmd/service/api.go b/cmd/service/api.go index 3689ccc..537e219 100644 --- a/cmd/service/api.go +++ b/cmd/service/api.go @@ -40,7 +40,7 @@ func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo { }) } - apiRoute := server.Group("/api", systemGlobalLock) + apiRoute := server.Group("/api", systemGlobalLock(custodialContainer)) apiRoute.POST("/account/create", api.HandleAccountCreate(custodialContainer)) apiRoute.GET("/account/status/:address", api.HandleNetworkAccountStatus(custodialContainer)) @@ -78,24 +78,22 @@ func customHTTPErrorHandler(err error, c echo.Context) { }) } -func systemGlobalLock(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - var ( - cu = c.Get("cu").(*custodial.Custodial) - ) +func systemGlobalLock(cu *custodial.Custodial) echo.MiddlewareFunc { + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + locked, err := cu.RedisClient.Get(c.Request().Context(), systemGlobalLockKey).Bool() + if err != nil { + return err + } - locked, err := cu.RedisClient.Get(c.Request().Context(), systemGlobalLockKey).Bool() - if err != nil { - return err + if locked { + return c.JSON(http.StatusServiceUnavailable, api.ErrResp{ + Ok: false, + Message: "System manually locked.", + }) + } + + return next(c) } - - if locked { - return c.JSON(http.StatusServiceUnavailable, api.ErrResp{ - Ok: false, - Message: "System manually locked.", - }) - } - - return next(c) } }