mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2026-05-24 04:06:18 +02:00
feat: add system global lock to be triggered manually
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user