Skip to content

Commit

Permalink
[Gateway] health check on gateway backend (#1728)
Browse files Browse the repository at this point in the history
* feat: endpoint to fetch testnet health status

* create rpcClient and tenClient to call health

* feat: format health response
  • Loading branch information
Jennievon authored Jan 8, 2024
1 parent 856e72e commit 74e0a39
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tools/walletextension/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func NewHTTPRoutes(walletExt *walletextension.WalletExtension) []Route {
Name: common.PathHealth,
Func: httpHandler(walletExt, healthRequestHandler),
},
{
Name: common.PathNetworkHealth,
Func: httpHandler(walletExt, networkHealthRequestHandler),
},
{
Name: common.PathVersion,
Func: httpHandler(walletExt, versionRequestHandler),
Expand Down Expand Up @@ -422,12 +426,40 @@ func healthRequestHandler(walletExt *walletextension.WalletExtension, conn userc
return
}

// TODO: connect to database and check if it is healthy
err = conn.WriteResponse([]byte(common.SuccessMsg))
if err != nil {
walletExt.Logger().Error("error writing success response", log.ErrKey, err)
}
}

// Handles request to /network-health endpoint.
func networkHealthRequestHandler(walletExt *walletextension.WalletExtension, userConn userconn.UserConn) {
// read the request
_, err := userConn.ReadRequest()
if err != nil {
walletExt.Logger().Error("error reading request", log.ErrKey, err)
return
}

healthStatus, err := walletExt.GetTenNodeHealthStatus()

data, err := json.Marshal(map[string]interface{}{
"result": healthStatus,
"error": err,
})
if err != nil {
walletExt.Logger().Error("error marshaling response", log.ErrKey, err)
return
}

err = userConn.WriteResponse(data)

if err != nil {
walletExt.Logger().Error("error writing success response", log.ErrKey, err)
}
}

// Handles request to /version endpoint.
func versionRequestHandler(walletExt *walletextension.WalletExtension, userConn userconn.UserConn) {
// read the request
Expand Down
1 change: 1 addition & 0 deletions tools/walletextension/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
PathRevoke = "/revoke/"
PathObscuroGateway = "/"
PathHealth = "/health/"
PathNetworkHealth = "/network-health/"
WSProtocol = "ws://"
DefaultUser = "defaultUser"
UserQueryParameter = "u"
Expand Down
13 changes: 13 additions & 0 deletions tools/walletextension/wallet_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ten-protocol/go-ten/tools/walletextension/config"

"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/obsclient"

"github.com/ten-protocol/go-ten/tools/walletextension/useraccountmanager"

Expand Down Expand Up @@ -38,6 +39,7 @@ type WalletExtension struct {
stopControl *stopcontrol.StopControl
version string
config *config.Config
tenClient *obsclient.ObsClient
}

func New(
Expand All @@ -49,6 +51,12 @@ func New(
logger gethlog.Logger,
config *config.Config,
) *WalletExtension {
rpcClient, err := rpc.NewNetworkClient(hostAddr)
if err != nil {
logger.Error(fmt.Errorf("could not create RPC client on %s. Cause: %w", hostAddr, err).Error())
panic(err)
}
newTenClient := obsclient.NewObsClient(rpcClient)
return &WalletExtension{
hostAddr: hostAddr,
userAccountManager: userAccountManager,
Expand All @@ -58,6 +66,7 @@ func New(
stopControl: stopControl,
version: version,
config: config,
tenClient: newTenClient,
}
}

Expand Down Expand Up @@ -384,3 +393,7 @@ func (w *WalletExtension) checkParametersForInterceptedGetStorageAt(params []int
func (w *WalletExtension) Version() string {
return w.version
}

func (w *WalletExtension) GetTenNodeHealthStatus() (bool, error) {
return w.tenClient.Health()
}

0 comments on commit 74e0a39

Please sign in to comment.