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 debugReportCaller: false
logDisableTimestamp: true logDisableTimestamp: true
logsPath: ./logs logsPath: ./logs
tracing:
mongoEnabled: true
mongoURI: "mongodb://root:example@mongo:27017/?maxPoolSize=20&w=majority"

View File

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

22
main.go
View File

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

View File

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