feat: add system global lock to be triggered manually

This commit is contained in:
2023-03-16 07:34:29 +00:00
parent ad58d1da47
commit ee907dddbc
4 changed files with 46 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"errors"
"net/http"
"time"
@@ -10,10 +11,12 @@ import (
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/redis/go-redis/v9"
)
const (
contextTimeout = 5 * time.Second
contextTimeout = 5 * time.Second
systemGlobalLockKey = "system:global_lock"
)
// Bootstrap API server.
@@ -47,6 +50,9 @@ func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
}
apiRoute := server.Group("/api")
apiRoute.Use(systemGlobalLock)
apiRoute.POST("/account/create", api.HandleAccountCreate)
apiRoute.POST("/sign/transfer", api.HandleSignTransfer)
apiRoute.GET("/track/:trackingId", api.HandleTrackTx)
@@ -81,3 +87,25 @@ func customHTTPErrorHandler(err error, c echo.Context) {
Message: "Internal server error.",
})
}
func systemGlobalLock(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
var (
cu = c.Get("cu").(*custodial.Custodial)
)
locked, err := cu.RedisClient.Get(c.Request().Context(), systemGlobalLockKey).Bool()
if !errors.Is(err, redis.Nil) {
return err
}
if locked {
return c.JSON(http.StatusOK, api.ErrResp{
Ok: false,
Message: "System manually locked.",
})
}
return next(c)
}
}