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" "time"
log "github.com/sirupsen/logrus" 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"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql/postgres" "github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql/postgres"
"github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper" "github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper"
) )
var ( var (
maxRetry = 5 // Max times to try to connect to the DB or BC at boot. maxRetry = 5 // Max times to try to connect to the DB or BC at boot.
retryInterval = 30 // The time to wait between each try. retryInterval = 30 // The time to wait between each try.
DB sql.Database = &postgres.DB{} DB sql.Database = &postgres.DB{}
BC *beaconclient.BeaconClient = &beaconclient.BeaconClient{}
) )
// A simple wrapper to create a DB object to use. // 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) { 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.Info("Booting the Application")
log.Debug("Checking beacon Client") log.Debug("Creating the Beacon Client")
err := healthcheck.CheckBeaconClient(bcAddress, bcPort) BC.Address = bcAddress
BC.Port = bcPort
log.Debug("Checking Beacon Client")
err := BC.CheckBeaconClient()
if err != nil { if err != nil {
return nil, err 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 ( import (
"testing" "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 ( import (
"fmt" "fmt"
@ -17,9 +17,9 @@ var (
// Keep in mind, the beacon client will allow you to connect to it but it might // 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 // 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 // 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") 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) resp, err := http.Get(bcEndpoint)
if err != nil { if err != nil {
loghelper.LogError(err).Error("Unable to get bc endpoint: ", bcEndpoint) loghelper.LogError(err).Error("Unable to get bc endpoint: ", bcEndpoint)

View File

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