From 7f00dbc1208f951da206c8605028158cab5d2be1 Mon Sep 17 00:00:00 2001 From: Marc Font <36164126+Marketen@users.noreply.github.com> Date: Wed, 15 May 2024 13:50:11 +0200 Subject: [PATCH] move mongo connection to main.go (#41) --- listener/cmd/listener/main.go | 14 +++++++++++++- listener/internal/api/api.go | 28 +++++++--------------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/listener/cmd/listener/main.go b/listener/cmd/listener/main.go index 0a19e04..2536506 100644 --- a/listener/cmd/listener/main.go +++ b/listener/cmd/listener/main.go @@ -4,6 +4,7 @@ import ( "github.com/dappnode/validator-monitoring/listener/internal/api" "github.com/dappnode/validator-monitoring/listener/internal/config" "github.com/dappnode/validator-monitoring/listener/internal/logger" + "github.com/dappnode/validator-monitoring/listener/internal/mongodb" "github.com/herumi/bls-eth-go-binary/bls" ) @@ -25,9 +26,20 @@ func main() { logger.Fatal("Failed to set BLS ETH mode: " + err.Error()) } + // Connect to MongoDB client & get the collection + dbClient, err := mongodb.GetMongoDbClient(config.MongoDBURI) + if err != nil { + logger.Fatal("Failed to connect to MongoDB: " + err.Error()) + } + dbCollection := dbClient.Database("validatorMonitoring").Collection("signatures") + if dbCollection == nil { + logger.Fatal("Failed to connect to MongoDB collection") + } + s := api.NewApi( config.Port, - config.MongoDBURI, + dbClient, + dbCollection, config.BeaconNodeURLs, config.BypassValidatorsFiltering, ) diff --git a/listener/internal/api/api.go b/listener/internal/api/api.go index 75c194e..8607999 100644 --- a/listener/internal/api/api.go +++ b/listener/internal/api/api.go @@ -5,28 +5,29 @@ import ( "github.com/dappnode/validator-monitoring/listener/internal/api/routes" "github.com/dappnode/validator-monitoring/listener/internal/logger" - "github.com/dappnode/validator-monitoring/listener/internal/mongodb" + "go.mongodb.org/mongo-driver/mongo" ) type httpApi struct { server *http.Server port string - dbUri string + dbClient *mongo.Client + dbCollection *mongo.Collection beaconNodeUrls map[string]string bypassValidatorFiltering bool } // create a new api instance -func NewApi(port string, mongoDbUri string, beaconNodeUrls map[string]string, bypassValidatorFiltering bool) *httpApi { +func NewApi(port string, dbClient *mongo.Client, dbCollection *mongo.Collection, beaconNodeUrls map[string]string, bypassValidatorFiltering bool) *httpApi { return &httpApi{ port: port, - dbUri: mongoDbUri, + dbClient: dbClient, + dbCollection: dbCollection, beaconNodeUrls: beaconNodeUrls, bypassValidatorFiltering: bypassValidatorFiltering, } } -// start the api func (s *httpApi) Start() { // if somehow s.server is not nil, it means the server is already running, this should never happen if s.server != nil { @@ -34,27 +35,12 @@ func (s *httpApi) Start() { } logger.Info("Server is running on port " + s.port) - var err error - // connect to the MongoDB server - dbClient, err := mongodb.GetMongoDbClient(s.dbUri) - if err != nil { - logger.Fatal("Failed to connect to MongoDB: " + err.Error()) - } - - // get the collection - dbCollection := dbClient.Database("validatorMonitoring").Collection("signatures") - if dbCollection == nil { - logger.Fatal("Failed to connect to MongoDB collection") - } - - // setup the http api s.server = &http.Server{ Addr: ":" + s.port, - Handler: routes.SetupRouter(dbCollection, s.beaconNodeUrls, s.bypassValidatorFiltering), + Handler: routes.SetupRouter(s.dbCollection, s.beaconNodeUrls, s.bypassValidatorFiltering), } - // start the api if err := s.server.ListenAndServe(); err != nil { logger.Fatal("Failed to start server: " + err.Error()) }