cic-custodial/cmd/service/api.go

106 lines
2.7 KiB
Go
Raw Permalink Normal View History

package main
import (
"net/http"
"github.com/VictoriaMetrics/metrics"
"github.com/go-playground/validator/v10"
_ "github.com/grassrootseconomics/cic-custodial/docs"
"github.com/grassrootseconomics/cic-custodial/internal/api"
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
"github.com/grassrootseconomics/cic-custodial/pkg/util"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
)
const (
major: breaking changes, fixes * add global lock * fix lock contention * update redis client and server -> v7 * add network status API * upgrade deps Squashed commit of the following: commit 9d95f2e8f8401819f11f33c00d4e2e72b67576a6 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 11:12:06 2023 +0000 feat: add updated_at postgres fn + trigger * closes #66 commit 144d5018ea011d4d4b12479c66f2a5087e8fc2a7 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 08:48:45 2023 +0000 feat: add network account status (nonce, balance) commit 5679a675f345b151fd6b079c799f6849800822fa Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 08:01:58 2023 +0000 fix: system global lock * add middleware to entire API group * setNX system lock key commit ee907dddbc0500f92bcb8e7274b186c970198a02 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 07:34:29 2023 +0000 feat: add system global lock to be triggered manually commit ad58d1da47c7f1fae2e400fe713b0ea07f32b416 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 16:02:25 2023 +0000 feat: add lock retry strategy * previouly we relied on the task being re-queued which generally reduces the throughput of tasks commit f4e3aedf336b378a8c0ca6b6080af9dc54e435cc Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 15:45:06 2023 +0000 tasker: add support for reporting panics commit b8ebf88f3692bf0cebae476d010cd7ff2656e11d Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 15:40:50 2023 +0000 pkg: bump go-redis -> v9 commit 4a0bf88322ebb79f86c1356685cf4abac0a0d17b Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:15:05 2023 +0300 build(deps): bump github.com/jackc/tern/v2 from 2.0.0 to 2.0.1 (#69) Bumps [github.com/jackc/tern/v2](https://github.com/jackc/tern) from 2.0.0 to 2.0.1. - [Release notes](https://github.com/jackc/tern/releases) - [Changelog](https://github.com/jackc/tern/blob/master/.goreleaser.yaml) - [Commits](https://github.com/jackc/tern/compare/v2.0.0...v2.0.1) --- updated-dependencies: - dependency-name: github.com/jackc/tern/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> commit 5328d271c11f1f34d9e69e4cdfa95a010bcba866 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:14:45 2023 +0300 build(deps): bump golang.org/x/crypto from 0.6.0 to 0.7.0 (#70) Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.6.0 to 0.7.0. - [Release notes](https://github.com/golang/crypto/releases) - [Commits](https://github.com/golang/crypto/compare/v0.6.0...v0.7.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> commit 7ce80f9e6dcc2752a03c78b7ad3f91f4cc0afed0 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:13:59 2023 +0300 build(deps): bump github.com/grassrootseconomics/celoutils (#71) Bumps [github.com/grassrootseconomics/celoutils](https://github.com/grassrootseconomics/celoutils) from 1.0.0 to 1.1.1. - [Release notes](https://github.com/grassrootseconomics/celoutils/releases) - [Commits](https://github.com/grassrootseconomics/celoutils/compare/v1.0.0...v1.1.1) --- updated-dependencies: - dependency-name: github.com/grassrootseconomics/celoutils dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-16 13:03:51 +01:00
systemGlobalLockKey = "system:global_lock"
)
// Bootstrap API server.
func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
customValidator := validator.New()
server := echo.New()
server.HideBanner = true
server.HidePort = true
server.Validator = &api.Validator{
ValidatorProvider: customValidator,
}
server.HTTPErrorHandler = customHTTPErrorHandler
server.Use(middleware.Recover())
server.Use(middleware.BodyLimit("1M"))
server.Use(middleware.ContextTimeout(util.SLATimeout))
if ko.Bool("service.metrics") {
server.GET("/metrics", func(c echo.Context) error {
metrics.WritePrometheus(c.Response(), true)
return nil
})
}
if ko.Bool("service.docs") {
server.GET("/docs/*", echoSwagger.WrapHandler)
}
apiRoute := server.Group("/api", systemGlobalLock(custodialContainer))
major: breaking changes, fixes * add global lock * fix lock contention * update redis client and server -> v7 * add network status API * upgrade deps Squashed commit of the following: commit 9d95f2e8f8401819f11f33c00d4e2e72b67576a6 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 11:12:06 2023 +0000 feat: add updated_at postgres fn + trigger * closes #66 commit 144d5018ea011d4d4b12479c66f2a5087e8fc2a7 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 08:48:45 2023 +0000 feat: add network account status (nonce, balance) commit 5679a675f345b151fd6b079c799f6849800822fa Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 08:01:58 2023 +0000 fix: system global lock * add middleware to entire API group * setNX system lock key commit ee907dddbc0500f92bcb8e7274b186c970198a02 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 07:34:29 2023 +0000 feat: add system global lock to be triggered manually commit ad58d1da47c7f1fae2e400fe713b0ea07f32b416 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 16:02:25 2023 +0000 feat: add lock retry strategy * previouly we relied on the task being re-queued which generally reduces the throughput of tasks commit f4e3aedf336b378a8c0ca6b6080af9dc54e435cc Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 15:45:06 2023 +0000 tasker: add support for reporting panics commit b8ebf88f3692bf0cebae476d010cd7ff2656e11d Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 15:40:50 2023 +0000 pkg: bump go-redis -> v9 commit 4a0bf88322ebb79f86c1356685cf4abac0a0d17b Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:15:05 2023 +0300 build(deps): bump github.com/jackc/tern/v2 from 2.0.0 to 2.0.1 (#69) Bumps [github.com/jackc/tern/v2](https://github.com/jackc/tern) from 2.0.0 to 2.0.1. - [Release notes](https://github.com/jackc/tern/releases) - [Changelog](https://github.com/jackc/tern/blob/master/.goreleaser.yaml) - [Commits](https://github.com/jackc/tern/compare/v2.0.0...v2.0.1) --- updated-dependencies: - dependency-name: github.com/jackc/tern/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> commit 5328d271c11f1f34d9e69e4cdfa95a010bcba866 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:14:45 2023 +0300 build(deps): bump golang.org/x/crypto from 0.6.0 to 0.7.0 (#70) Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.6.0 to 0.7.0. - [Release notes](https://github.com/golang/crypto/releases) - [Commits](https://github.com/golang/crypto/compare/v0.6.0...v0.7.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> commit 7ce80f9e6dcc2752a03c78b7ad3f91f4cc0afed0 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:13:59 2023 +0300 build(deps): bump github.com/grassrootseconomics/celoutils (#71) Bumps [github.com/grassrootseconomics/celoutils](https://github.com/grassrootseconomics/celoutils) from 1.0.0 to 1.1.1. - [Release notes](https://github.com/grassrootseconomics/celoutils/releases) - [Commits](https://github.com/grassrootseconomics/celoutils/compare/v1.0.0...v1.1.1) --- updated-dependencies: - dependency-name: github.com/grassrootseconomics/celoutils dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-16 13:03:51 +01:00
apiRoute.POST("/account/create", api.HandleAccountCreate(custodialContainer))
apiRoute.GET("/account/status/:address", api.HandleNetworkAccountStatus(custodialContainer))
apiRoute.POST("/sign/transfer", api.HandleSignTransfer(custodialContainer))
apiRoute.POST("/sign/transferAuth", api.HandleSignTranserAuthorization(custodialContainer))
apiRoute.GET("/track/:trackingId", api.HandleTrackTx(custodialContainer))
return server
}
func customHTTPErrorHandler(err error, c echo.Context) {
if c.Response().Committed {
return
}
if he, ok := err.(*echo.HTTPError); ok {
var errorMsg string
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,
})
return
}
lo.Error("api: echo error", "path", c.Path(), "err", err)
c.JSON(http.StatusInternalServerError, api.ErrResp{
Ok: false,
Message: "Internal server error.",
})
}
major: breaking changes, fixes * add global lock * fix lock contention * update redis client and server -> v7 * add network status API * upgrade deps Squashed commit of the following: commit 9d95f2e8f8401819f11f33c00d4e2e72b67576a6 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 11:12:06 2023 +0000 feat: add updated_at postgres fn + trigger * closes #66 commit 144d5018ea011d4d4b12479c66f2a5087e8fc2a7 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 08:48:45 2023 +0000 feat: add network account status (nonce, balance) commit 5679a675f345b151fd6b079c799f6849800822fa Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 08:01:58 2023 +0000 fix: system global lock * add middleware to entire API group * setNX system lock key commit ee907dddbc0500f92bcb8e7274b186c970198a02 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 07:34:29 2023 +0000 feat: add system global lock to be triggered manually commit ad58d1da47c7f1fae2e400fe713b0ea07f32b416 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 16:02:25 2023 +0000 feat: add lock retry strategy * previouly we relied on the task being re-queued which generally reduces the throughput of tasks commit f4e3aedf336b378a8c0ca6b6080af9dc54e435cc Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 15:45:06 2023 +0000 tasker: add support for reporting panics commit b8ebf88f3692bf0cebae476d010cd7ff2656e11d Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 15:40:50 2023 +0000 pkg: bump go-redis -> v9 commit 4a0bf88322ebb79f86c1356685cf4abac0a0d17b Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:15:05 2023 +0300 build(deps): bump github.com/jackc/tern/v2 from 2.0.0 to 2.0.1 (#69) Bumps [github.com/jackc/tern/v2](https://github.com/jackc/tern) from 2.0.0 to 2.0.1. - [Release notes](https://github.com/jackc/tern/releases) - [Changelog](https://github.com/jackc/tern/blob/master/.goreleaser.yaml) - [Commits](https://github.com/jackc/tern/compare/v2.0.0...v2.0.1) --- updated-dependencies: - dependency-name: github.com/jackc/tern/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> commit 5328d271c11f1f34d9e69e4cdfa95a010bcba866 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:14:45 2023 +0300 build(deps): bump golang.org/x/crypto from 0.6.0 to 0.7.0 (#70) Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.6.0 to 0.7.0. - [Release notes](https://github.com/golang/crypto/releases) - [Commits](https://github.com/golang/crypto/compare/v0.6.0...v0.7.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> commit 7ce80f9e6dcc2752a03c78b7ad3f91f4cc0afed0 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:13:59 2023 +0300 build(deps): bump github.com/grassrootseconomics/celoutils (#71) Bumps [github.com/grassrootseconomics/celoutils](https://github.com/grassrootseconomics/celoutils) from 1.0.0 to 1.1.1. - [Release notes](https://github.com/grassrootseconomics/celoutils/releases) - [Commits](https://github.com/grassrootseconomics/celoutils/compare/v1.0.0...v1.1.1) --- updated-dependencies: - dependency-name: github.com/grassrootseconomics/celoutils dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-16 13:03:51 +01: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)
major: breaking changes, fixes * add global lock * fix lock contention * update redis client and server -> v7 * add network status API * upgrade deps Squashed commit of the following: commit 9d95f2e8f8401819f11f33c00d4e2e72b67576a6 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 11:12:06 2023 +0000 feat: add updated_at postgres fn + trigger * closes #66 commit 144d5018ea011d4d4b12479c66f2a5087e8fc2a7 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 08:48:45 2023 +0000 feat: add network account status (nonce, balance) commit 5679a675f345b151fd6b079c799f6849800822fa Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 08:01:58 2023 +0000 fix: system global lock * add middleware to entire API group * setNX system lock key commit ee907dddbc0500f92bcb8e7274b186c970198a02 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Mar 16 07:34:29 2023 +0000 feat: add system global lock to be triggered manually commit ad58d1da47c7f1fae2e400fe713b0ea07f32b416 Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 16:02:25 2023 +0000 feat: add lock retry strategy * previouly we relied on the task being re-queued which generally reduces the throughput of tasks commit f4e3aedf336b378a8c0ca6b6080af9dc54e435cc Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 15:45:06 2023 +0000 tasker: add support for reporting panics commit b8ebf88f3692bf0cebae476d010cd7ff2656e11d Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Tue Mar 14 15:40:50 2023 +0000 pkg: bump go-redis -> v9 commit 4a0bf88322ebb79f86c1356685cf4abac0a0d17b Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:15:05 2023 +0300 build(deps): bump github.com/jackc/tern/v2 from 2.0.0 to 2.0.1 (#69) Bumps [github.com/jackc/tern/v2](https://github.com/jackc/tern) from 2.0.0 to 2.0.1. - [Release notes](https://github.com/jackc/tern/releases) - [Changelog](https://github.com/jackc/tern/blob/master/.goreleaser.yaml) - [Commits](https://github.com/jackc/tern/compare/v2.0.0...v2.0.1) --- updated-dependencies: - dependency-name: github.com/jackc/tern/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> commit 5328d271c11f1f34d9e69e4cdfa95a010bcba866 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:14:45 2023 +0300 build(deps): bump golang.org/x/crypto from 0.6.0 to 0.7.0 (#70) Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.6.0 to 0.7.0. - [Release notes](https://github.com/golang/crypto/releases) - [Commits](https://github.com/golang/crypto/compare/v0.6.0...v0.7.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> commit 7ce80f9e6dcc2752a03c78b7ad3f91f4cc0afed0 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue Mar 14 18:13:59 2023 +0300 build(deps): bump github.com/grassrootseconomics/celoutils (#71) Bumps [github.com/grassrootseconomics/celoutils](https://github.com/grassrootseconomics/celoutils) from 1.0.0 to 1.1.1. - [Release notes](https://github.com/grassrootseconomics/celoutils/releases) - [Commits](https://github.com/grassrootseconomics/celoutils/compare/v1.0.0...v1.1.1) --- updated-dependencies: - dependency-name: github.com/grassrootseconomics/celoutils dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-16 13:03:51 +01:00
}
}
}