MongoDB configuration by file property, and added timeout connection MongoDB

This commit is contained in:
Mario
2022-05-17 23:31:20 +02:00
parent 296cf26861
commit 50705243f4
4 changed files with 34 additions and 17 deletions

View File

@ -4,3 +4,6 @@ core:
debugReportCaller: false
logDisableTimestamp: true
logsPath: ./logs
tracing:
mongoEnabled: true
mongoURI: "mongodb://root:example@mongo:27017/?maxPoolSize=20&w=majority"

View File

@ -23,7 +23,7 @@ services:
restart: always
container_name: mongo-express
ports:
- 8081:8081
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example

38
main.go
View File

@ -12,12 +12,11 @@ import (
"go.mongodb.org/mongo-driver/mongo/readpref"
"io"
"os"
"time"
)
var quit = make(chan struct{})
const mongoURI = "mongodb://root:example@mongo:27017/?maxPoolSize=20&w=majority"
var mongoClient *mongo.Client
func main() {
@ -43,8 +42,15 @@ func main() {
// Init protocol manager, with simple log on stout trace strategy and default protocol HTTP
protocolManager := protocols.InitProtocolManager(traceStrategyStdout, hypertextTransferProtocolStrategy)
mongoClient = buildMongoClient(mongoURI)
defer mongoClient.Disconnect(context.TODO())
if coreConfigurations.Core.Tracing.MongoEnabled {
mongoClient = buildMongoClient(coreConfigurations.Core.Tracing.MongoURI)
defer func(mongoClient *mongo.Client, ctx context.Context) {
err := mongoClient.Disconnect(ctx)
if err != nil {
log.Error(err)
}
}(mongoClient, context.TODO())
}
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
switch beelzebubServiceConfiguration.Protocol {
@ -68,19 +74,21 @@ func main() {
}
func traceStrategyStdout(event tracer.Event) {
log.WithFields(log.Fields{
"status": event.Status.String(),
"status": event.Status,
"event": event,
}).Info("New Event")
coll := mongoClient.Database("beelzebub").Collection("event")
data, err := bson.Marshal(event)
if err != nil {
log.Fatal(err)
}
if mongoClient != nil {
coll := mongoClient.Database("beelzebub").Collection("event")
data, err := bson.Marshal(event)
if err != nil {
log.Fatal(err)
}
_, err = coll.InsertOne(context.TODO(), data)
if err != nil {
log.Fatal(err)
_, err = coll.InsertOne(context.TODO(), data)
if err != nil {
log.Fatal(err)
}
}
}
@ -106,13 +114,13 @@ func configureLoggingByConfigurations(configurations parser.Logging) *os.File {
func buildMongoClient(uri string) *mongo.Client {
// Create a new client and connect to the server
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetServerSelectionTimeout(time.Second*2))
if err != nil {
log.Fatal(err)
}
// Ping the primary
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
panic(err)
log.Fatal(err)
}
log.Println("Successfully connected and pinged.")
return client

View File

@ -12,6 +12,7 @@ import (
type BeelzebubCoreConfigurations struct {
Core struct {
Logging Logging `yaml:"logging"`
Tracing Tracing `yaml:"tracing"`
}
}
@ -22,6 +23,11 @@ type Logging struct {
LogsPath string `yaml:"logsPath,omitempty"`
}
type Tracing struct {
MongoEnabled bool `yaml:"mongoEnabled,omitempty"`
MongoURI string `yaml:"mongoURI,omitempty"`
}
type BeelzebubServiceConfiguration struct {
ApiVersion string `yaml:"apiVersion"`
Protocol string `yaml:"protocol"`