Skip to content

Commit

Permalink
move mongo connection to main.go (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketen authored May 15, 2024
1 parent 2c2f919 commit 7f00dbc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
14 changes: 13 additions & 1 deletion listener/cmd/listener/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
)
Expand Down
28 changes: 7 additions & 21 deletions listener/internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,42 @@ 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 {
logger.Fatal("HTTP server already started")
}

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())
}
Expand Down

0 comments on commit 7f00dbc

Please sign in to comment.