Skip to content

Commit

Permalink
Add an health endpoint to OG (#1427)
Browse files Browse the repository at this point in the history
* Add an health endpoint to OG

* lint

* Update integration/obscurogateway/obscurogateway_test.go
  • Loading branch information
otherview authored Aug 7, 2023
1 parent 3d4fabe commit bcf05b2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
30 changes: 28 additions & 2 deletions integration/obscurogateway/obscurogateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"context"
"fmt"
"math/big"
"net/http"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/valyala/fasthttp"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -66,8 +71,11 @@ func TestObscuroGateway(t *testing.T) {
time.Sleep(5 * time.Second)

// make sure the server is ready to receive requests
// TODO Implement health endpoint
// serverAddress := fmt.Sprintf("http://%s:%d", obscuroGatewayConf.WalletExtensionHost, obscuroGatewayConf.WalletExtensionPortHTTP)
serverAddress := fmt.Sprintf("http://%s:%d", obscuroGatewayConf.WalletExtensionHost, obscuroGatewayConf.WalletExtensionPortHTTP)

// make sure the server is ready to receive requests
err := waitServerIsReady(serverAddress)
require.NoError(t, err)

w := wallets.L2FaucetWallet

Expand Down Expand Up @@ -171,3 +179,21 @@ func createObscuroNetwork(t *testing.T, startPort int) *params.SimWallets {
}
return wallets
}

func waitServerIsReady(serverAddr string) error {
for now := time.Now(); time.Since(now) < 30*time.Second; time.Sleep(500 * time.Millisecond) {
statusCode, _, err := fasthttp.Get(nil, fmt.Sprintf("%s/health/", serverAddr))
if err != nil {
// give it time to boot up
if strings.Contains(err.Error(), "connection") {
continue
}
return err
}

if statusCode == http.StatusOK {
return nil
}
}
return fmt.Errorf("timed out before server was ready")
}
19 changes: 19 additions & 0 deletions tools/walletextension/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func NewHTTPRoutes(walletExt *walletextension.WalletExtension) []Route {
Name: common.PathRevoke,
Func: httpHandler(walletExt, revokeRequestHandler),
},
{
Name: common.PathHealth,
Func: httpHandler(walletExt, healthRequestHandler),
},
}
}

Expand Down Expand Up @@ -415,3 +419,18 @@ func revokeRequestHandler(walletExt *walletextension.WalletExtension, userConn u
walletExt.Logger().Error(fmt.Errorf("error writing success response, %w", err).Error())
}
}

// Handles request to /health endpoint.
func healthRequestHandler(walletExt *walletextension.WalletExtension, userConn userconn.UserConn) {
// read the request
_, err := userConn.ReadRequest()
if err != nil {
walletExt.Logger().Error(fmt.Errorf("error reading request: %w", err).Error())
return
}

err = userConn.WriteResponse([]byte(common.SuccessMsg))
if err != nil {
walletExt.Logger().Error(fmt.Errorf("error writing success response, %w", err).Error())
}
}
1 change: 1 addition & 0 deletions tools/walletextension/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
PathAuthenticate = "/authenticate/"
PathQuery = "/query/"
PathRevoke = "/revoke/"
PathHealth = "/health/"
WSProtocol = "ws://"
DefaultUser = "defaultUser"
UserQueryParameter = "u"
Expand Down

0 comments on commit bcf05b2

Please sign in to comment.