2023-02-15 08:05:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-02-24 17:46:46 +01:00
|
|
|
"time"
|
2023-02-15 08:05:43 +01:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/go-playground/validator/v10"
|
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-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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-03-02 16:46:02 +01:00
|
|
|
contextTimeout = 5 * time.Second
|
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()
|
|
|
|
customValidator.RegisterValidation("eth_checksum", api.EthChecksumValidator)
|
|
|
|
|
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(func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
c.Set("cu", custodialContainer)
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
server.Use(middleware.Recover())
|
|
|
|
server.Use(middleware.BodyLimit("1M"))
|
2023-03-02 16:46:02 +01:00
|
|
|
server.Use(middleware.ContextTimeout(contextTimeout))
|
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-02-24 17:46:46 +01:00
|
|
|
apiRoute := server.Group("/api")
|
|
|
|
apiRoute.POST("/account/create", api.HandleAccountCreate)
|
|
|
|
apiRoute.POST("/sign/transfer", api.HandleSignTransfer)
|
|
|
|
apiRoute.GET("/track/:trackingId", api.HandleTrackTx)
|
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
|
|
|
}
|