Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move mongo connection to main.go #41

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading