feat: add system global lock to be triggered manually

This commit is contained in:
Mohamed Sohail 2023-03-16 07:34:29 +00:00
parent ad58d1da47
commit ee907dddbc
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
4 changed files with 46 additions and 6 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
cic-custodial cic-custodial
dist dist
.env **/*.env
covprofile covprofile

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"net/http" "net/http"
"time" "time"
@ -10,10 +11,12 @@ import (
"github.com/grassrootseconomics/cic-custodial/internal/custodial" "github.com/grassrootseconomics/cic-custodial/internal/custodial"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
"github.com/redis/go-redis/v9"
) )
const ( const (
contextTimeout = 5 * time.Second contextTimeout = 5 * time.Second
systemGlobalLockKey = "system:global_lock"
) )
// Bootstrap API server. // Bootstrap API server.
@ -47,6 +50,9 @@ func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
} }
apiRoute := server.Group("/api") apiRoute := server.Group("/api")
apiRoute.Use(systemGlobalLock)
apiRoute.POST("/account/create", api.HandleAccountCreate) apiRoute.POST("/account/create", api.HandleAccountCreate)
apiRoute.POST("/sign/transfer", api.HandleSignTransfer) apiRoute.POST("/sign/transfer", api.HandleSignTransfer)
apiRoute.GET("/track/:trackingId", api.HandleTrackTx) apiRoute.GET("/track/:trackingId", api.HandleTrackTx)
@ -81,3 +87,25 @@ func customHTTPErrorHandler(err error, c echo.Context) {
Message: "Internal server error.", 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)
}
}

View File

@ -1,7 +1,7 @@
version: "3.9" version: "3.9"
services: services:
redis: redis:
image: redis:6-alpine image: redis:7-alpine
restart: unless-stopped restart: unless-stopped
command: redis-server --save 60 1 --loglevel warning command: redis-server --save 60 1 --loglevel warning
volumes: volumes:
@ -20,9 +20,9 @@ services:
environment: environment:
- POSTGRES_PASSWORD=postgres - POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres - POSTGRES_USER=postgres
- POSTGRES_DB=cic_custodial
volumes: volumes:
- cic-custodial-pg:/var/lib/postgresql/data - cic-custodial-pg:/var/lib/postgresql/data
- ./init_db.sql:/docker-entrypoint-initdb.d/init_db.sql
ports: ports:
- "127.0.0.1:5432:5432" - "127.0.0.1:5432:5432"
healthcheck: healthcheck:
@ -37,8 +37,8 @@ services:
volumes: volumes:
- cic-custodial-nats:/nats/data - cic-custodial-nats:/nats/data
ports: ports:
- "4222:4222" - "127.0.0.1:4222:4222"
- "8222:8222" - "127.0.0.1:8222:8222"
asynqmon: asynqmon:
image: hibiken/asynqmon image: hibiken/asynqmon
restart: unless-stopped restart: unless-stopped
@ -49,6 +49,16 @@ services:
depends_on: depends_on:
redis: redis:
condition: service_healthy condition: service_healthy
cic-chain-events:
image: ghcr.io/grassrootseconomics/cic-chain-events/cic-chain-events:latest
restart: unless-stopped
env_file:
- events.env
ports:
- '127.0.0.1:5001:5000'
depends_on:
postgres:
condition: service_healthy
volumes: volumes:
cic-custodial-pg: cic-custodial-pg:
driver: local driver: local

2
dev/init_db.sql Normal file
View File

@ -0,0 +1,2 @@
CREATE DATABASE cic_custodial;
CREATE DATABASE cic_chain_events;