Create a single beaconclient pkg instead of having subpackages

The main reason to do this is so they can all access the `BeaconClient` Struct.
This commit is contained in:
Abdul Rabbani 2022-04-25 11:32:46 -04:00
parent 13ada14860
commit 5b75f5a257
6 changed files with 40 additions and 16 deletions

View File

@ -4,16 +4,17 @@ import (
"time"
log "github.com/sirupsen/logrus"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient/healthcheck"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql/postgres"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper"
)
var (
maxRetry = 5 // Max times to try to connect to the DB or BC at boot.
retryInterval = 30 // The time to wait between each try.
DB sql.Database = &postgres.DB{}
maxRetry = 5 // Max times to try to connect to the DB or BC at boot.
retryInterval = 30 // The time to wait between each try.
DB sql.Database = &postgres.DB{}
BC *beaconclient.BeaconClient = &beaconclient.BeaconClient{}
)
// A simple wrapper to create a DB object to use.
@ -57,8 +58,12 @@ func SetupPostgresDb(dbHostname string, dbPort int, dbName string, dbUsername st
func BootApplication(dbHostname string, dbPort int, dbName string, dbUsername string, dbPassword string, driverName string, bcAddress string, bcPort int) (sql.Database, error) {
log.Info("Booting the Application")
log.Debug("Checking beacon Client")
err := healthcheck.CheckBeaconClient(bcAddress, bcPort)
log.Debug("Creating the Beacon Client")
BC.Address = bcAddress
BC.Port = bcPort
log.Debug("Checking Beacon Client")
err := BC.CheckBeaconClient()
if err != nil {
return nil, err
}

View File

@ -0,0 +1,6 @@
package beaconclient
type BeaconClient struct {
Address string
Port int
}

View File

@ -1,4 +1,4 @@
package healthcheck_test
package beaconclient_test
import (
"testing"

View File

@ -0,0 +1,7 @@
// This package will handle all event subscriptions that utilize SSE.
package beaconclient
func ListenToHead() {
}

View File

@ -1,4 +1,4 @@
package healthcheck
package beaconclient
import (
"fmt"
@ -17,9 +17,9 @@ var (
// Keep in mind, the beacon client will allow you to connect to it but it might
// Not allow you to make http requests. This is part of its built in logic, and you will have
// to follow their provided guidelines. https://lighthouse-book.sigmaprime.io/api-bn.html#security
func CheckBeaconClient(bcAddress string, bcPort int) error {
func (bc *BeaconClient) CheckBeaconClient() error {
log.Debug("Attempting to connect to the beacon client")
bcEndpoint := "http://" + bcAddress + ":" + strconv.Itoa(bcPort) + bcHealthEndpoint
bcEndpoint := "http://" + bc.Address + ":" + strconv.Itoa(bc.Port) + bcHealthEndpoint
resp, err := http.Get(bcEndpoint)
if err != nil {
loghelper.LogError(err).Error("Unable to get bc endpoint: ", bcEndpoint)

View File

@ -1,26 +1,32 @@
package healthcheck_test
package beaconclient_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient/healthcheck"
beaconclient "github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient"
)
var _ = Describe("Healthcheck", func() {
var (
bcAddress string = "localhost"
bcPort int = 5052
BC = beaconclient.BeaconClient{
Address: "localhost",
Port: 5052,
}
errBc = beaconclient.BeaconClient{
Address: "blah",
Port: 10,
}
)
Describe("Connecting to the lighthouse client", Label("integration"), func() {
Context("When the client is running", func() {
It("We should connect successfully", func() {
err := healthcheck.CheckBeaconClient(bcAddress, bcPort)
err := BC.CheckBeaconClient()
Expect(err).To(BeNil())
})
})
Context("When the client is running", func() {
It("We should connect successfully", func() {
err := healthcheck.CheckBeaconClient("blah-blah", 10)
err := errBc.CheckBeaconClient()
Expect(err).ToNot(BeNil())
})
})