From 5b75f5a2570be74fe6a5bfa49467d3579ad6098a Mon Sep 17 00:00:00 2001 From: Abdul Rabbani Date: Mon, 25 Apr 2022 11:32:46 -0400 Subject: [PATCH] Create a single `beaconclient` `pkg` instead of having subpackages The main reason to do this is so they can all access the `BeaconClient` Struct. --- internal/boot/boot.go | 17 +++++++++++------ pkg/beaconclient/beaconclient_struct.go | 6 ++++++ ...uite_test.go => beaconclient_suite_test.go} | 2 +- pkg/beaconclient/head_topic.go | 7 +++++++ .../{healthcheck => }/healthcheck.go | 6 +++--- .../{healthcheck => }/healthcheck_test.go | 18 ++++++++++++------ 6 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 pkg/beaconclient/beaconclient_struct.go rename pkg/beaconclient/{healthcheck/healthcheck_suite_test.go => beaconclient_suite_test.go} (87%) create mode 100644 pkg/beaconclient/head_topic.go rename pkg/beaconclient/{healthcheck => }/healthcheck.go (87%) rename pkg/beaconclient/{healthcheck => }/healthcheck_test.go (59%) diff --git a/internal/boot/boot.go b/internal/boot/boot.go index 86bd891..9d6c4a8 100644 --- a/internal/boot/boot.go +++ b/internal/boot/boot.go @@ -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 } diff --git a/pkg/beaconclient/beaconclient_struct.go b/pkg/beaconclient/beaconclient_struct.go new file mode 100644 index 0000000..0e9b00d --- /dev/null +++ b/pkg/beaconclient/beaconclient_struct.go @@ -0,0 +1,6 @@ +package beaconclient + +type BeaconClient struct { + Address string + Port int +} diff --git a/pkg/beaconclient/healthcheck/healthcheck_suite_test.go b/pkg/beaconclient/beaconclient_suite_test.go similarity index 87% rename from pkg/beaconclient/healthcheck/healthcheck_suite_test.go rename to pkg/beaconclient/beaconclient_suite_test.go index 510e24f..4c9c1d3 100644 --- a/pkg/beaconclient/healthcheck/healthcheck_suite_test.go +++ b/pkg/beaconclient/beaconclient_suite_test.go @@ -1,4 +1,4 @@ -package healthcheck_test +package beaconclient_test import ( "testing" diff --git a/pkg/beaconclient/head_topic.go b/pkg/beaconclient/head_topic.go new file mode 100644 index 0000000..1db229d --- /dev/null +++ b/pkg/beaconclient/head_topic.go @@ -0,0 +1,7 @@ +// This package will handle all event subscriptions that utilize SSE. + +package beaconclient + +func ListenToHead() { + +} diff --git a/pkg/beaconclient/healthcheck/healthcheck.go b/pkg/beaconclient/healthcheck.go similarity index 87% rename from pkg/beaconclient/healthcheck/healthcheck.go rename to pkg/beaconclient/healthcheck.go index 2481bfc..bf6b562 100644 --- a/pkg/beaconclient/healthcheck/healthcheck.go +++ b/pkg/beaconclient/healthcheck.go @@ -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) diff --git a/pkg/beaconclient/healthcheck/healthcheck_test.go b/pkg/beaconclient/healthcheck_test.go similarity index 59% rename from pkg/beaconclient/healthcheck/healthcheck_test.go rename to pkg/beaconclient/healthcheck_test.go index f6f406a..ebebd0c 100644 --- a/pkg/beaconclient/healthcheck/healthcheck_test.go +++ b/pkg/beaconclient/healthcheck_test.go @@ -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()) }) })