Skip to content

Commit

Permalink
Merge pull request #2 from anika308142/master
Browse files Browse the repository at this point in the history
fixing mongo local connection
  • Loading branch information
Shipu committed Mar 13, 2022
2 parents a4bd134 + 4cc29d9 commit 13efe10
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,24 @@ func (mongoCollection MongoCollection) WithContext() MongoCollection {
}

func NewMongoDB() *MongoDB {
port := Config.GetString("DB.Port")
var mongoProtocol, host string

if port != "" {
mongoProtocol = "mongodb://"
host = Config.GetString("DB.Host") + ":" + port
} else {
mongoProtocol = "mongodb+srv://"
host = Config.GetString("DB.Host")
}

password := Config.GetString("DB.Password")
mongoUserInfo := ""
if password != "" {
mongoUserInfo = Config.GetString("DB.Username") + ":" + password + "@"
}

mongoUri := "mongodb+srv://" + Config.GetString("DB.Username") + ":" + Config.GetString("DB.Password") + "@" + Config.GetString("DB.Host") + "/" + Config.GetString("DB.Database") + "?retryWrites=true&w=majority"
mongoUri := mongoProtocol + mongoUserInfo + host + "/" + Config.GetString("DB.Database") + "?retryWrites=true&w=majority"

client, err := mongo.NewClient(options.Client().ApplyURI(mongoUri))
if err != nil {
Expand Down

0 comments on commit 13efe10

Please sign in to comment.