mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-10 01:06:46 +01:00
Mohammed Sohail
cf1f9f34c3
* fallback to custom ethereum checksum validator -> https://github.com/go-playground/validator/issues/1073 * decouple jetsream emitter to separate package * refactor task handlers into individual files * add error handler for echo to capture unexpected errors and log them * move handler dependencies into single struct container -> custodialContainer * replace signer to use EIP 1559 signer -> celoutils v1 * Add 1 minutes timeout to all custodial tasks
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/grassrootseconomics/cic-custodial/internal/api"
|
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
|
"github.com/hibiken/asynq"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// Bootstrap API server.
|
|
func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
|
|
lo.Debug("api: bootstrapping api server")
|
|
server := echo.New()
|
|
server.HideBanner = true
|
|
server.HidePort = true
|
|
|
|
server.HTTPErrorHandler = func(err error, c echo.Context) {
|
|
// Handle asynq duplication errors across all api handlers.
|
|
if errors.Is(err, asynq.ErrTaskIDConflict) {
|
|
c.JSON(http.StatusForbidden, api.ErrResp{
|
|
Ok: false,
|
|
Code: api.DUPLICATE_ERROR,
|
|
Message: "Request with duplicate tracking id submitted.",
|
|
})
|
|
return
|
|
}
|
|
|
|
if err.(validator.ValidationErrors) != nil {
|
|
c.JSON(http.StatusForbidden, api.ErrResp{
|
|
Ok: false,
|
|
Code: api.VALIDATION_ERROR,
|
|
Message: err.(validator.ValidationErrors).Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Log internal server error for further investigation.
|
|
lo.Error("api:", "path", c.Path(), "err", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, api.ErrResp{
|
|
Ok: false,
|
|
Code: api.INTERNAL_ERROR,
|
|
Message: "Internal server error.",
|
|
})
|
|
}
|
|
|
|
if ko.Bool("service.metrics") {
|
|
server.GET("/metrics", func(c echo.Context) error {
|
|
metrics.WritePrometheus(c.Response(), true)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
customValidator := validator.New()
|
|
customValidator.RegisterValidation("eth_checksum", api.EthChecksumValidator)
|
|
|
|
server.Validator = &api.Validator{
|
|
ValidatorProvider: customValidator,
|
|
}
|
|
|
|
apiRoute := server.Group("/api")
|
|
apiRoute.POST("/account/create", api.CreateAccountHandler(custodialContainer))
|
|
apiRoute.POST("/sign/transfer", api.SignTransferHandler(custodialContainer))
|
|
|
|
return server
|
|
}
|