2023-02-15 08:05:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/go-playground/validator/v10"
|
2023-04-13 13:55:07 +02:00
|
|
|
_ "github.com/grassrootseconomics/cic-custodial/docs"
|
2023-02-15 08:05:43 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/api"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
2023-04-11 12:14:49 +02:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/util"
|
2023-02-15 08:05:43 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-02-24 17:46:46 +01:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-04-13 13:55:07 +02:00
|
|
|
echoSwagger "github.com/swaggo/echo-swagger"
|
2023-02-24 17:46:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-03-16 13:03:51 +01:00
|
|
|
systemGlobalLockKey = "system:global_lock"
|
2023-02-15 08:05:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Bootstrap API server.
|
2023-02-20 10:56:30 +01:00
|
|
|
func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
|
2023-02-24 17:46:46 +01:00
|
|
|
customValidator := validator.New()
|
|
|
|
|
2023-02-15 08:05:43 +01:00
|
|
|
server := echo.New()
|
|
|
|
server.HideBanner = true
|
|
|
|
server.HidePort = true
|
2023-02-24 17:46:46 +01:00
|
|
|
server.Validator = &api.Validator{
|
|
|
|
ValidatorProvider: customValidator,
|
|
|
|
}
|
|
|
|
server.HTTPErrorHandler = customHTTPErrorHandler
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-02-24 17:46:46 +01:00
|
|
|
server.Use(middleware.Recover())
|
|
|
|
server.Use(middleware.BodyLimit("1M"))
|
2023-04-11 12:14:49 +02:00
|
|
|
server.Use(middleware.ContextTimeout(util.SLATimeout))
|
2023-02-15 08:05:43 +01:00
|
|
|
|
|
|
|
if ko.Bool("service.metrics") {
|
|
|
|
server.GET("/metrics", func(c echo.Context) error {
|
|
|
|
metrics.WritePrometheus(c.Response(), true)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-13 13:55:07 +02:00
|
|
|
if ko.Bool("service.docs") {
|
|
|
|
server.GET("/docs/*", echoSwagger.WrapHandler)
|
|
|
|
}
|
|
|
|
|
2023-03-28 08:55:51 +02:00
|
|
|
apiRoute := server.Group("/api", systemGlobalLock(custodialContainer))
|
2023-03-16 13:03:51 +01:00
|
|
|
|
2023-03-28 08:39:44 +02:00
|
|
|
apiRoute.POST("/account/create", api.HandleAccountCreate(custodialContainer))
|
|
|
|
apiRoute.GET("/account/status/:address", api.HandleNetworkAccountStatus(custodialContainer))
|
|
|
|
apiRoute.POST("/sign/transfer", api.HandleSignTransfer(custodialContainer))
|
2023-07-10 08:32:05 +02:00
|
|
|
apiRoute.POST("/sign/transferAuth", api.HandleSignTranserAuthorization(custodialContainer))
|
2023-03-28 08:39:44 +02:00
|
|
|
apiRoute.GET("/track/:trackingId", api.HandleTrackTx(custodialContainer))
|
2023-02-20 10:56:30 +01:00
|
|
|
|
2023-02-24 17:46:46 +01:00
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
|
|
|
func customHTTPErrorHandler(err error, c echo.Context) {
|
|
|
|
if c.Response().Committed {
|
|
|
|
return
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-02 16:46:02 +01:00
|
|
|
if he, ok := err.(*echo.HTTPError); ok {
|
2023-02-24 17:46:46 +01:00
|
|
|
var errorMsg string
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-02-24 17:46:46 +01:00
|
|
|
if m, ok := he.Message.(error); ok {
|
|
|
|
errorMsg = m.Error()
|
|
|
|
} else if m, ok := he.Message.(string); ok {
|
|
|
|
errorMsg = m
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(he.Code, api.ErrResp{
|
|
|
|
Ok: false,
|
|
|
|
Message: errorMsg,
|
|
|
|
})
|
2023-03-02 16:46:02 +01:00
|
|
|
return
|
2023-02-24 17:46:46 +01:00
|
|
|
}
|
2023-03-02 16:46:02 +01:00
|
|
|
|
|
|
|
lo.Error("api: echo error", "path", c.Path(), "err", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, api.ErrResp{
|
|
|
|
Ok: false,
|
|
|
|
Message: "Internal server error.",
|
|
|
|
})
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|
2023-03-16 13:03:51 +01:00
|
|
|
|
2023-03-28 08:55:51 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if locked {
|
|
|
|
return c.JSON(http.StatusServiceUnavailable, api.ErrResp{
|
|
|
|
Ok: false,
|
|
|
|
Message: "System manually locked.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(c)
|
2023-03-16 13:03:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|