Add ability to supply Mongo connection uri

This commit is contained in:
Kalissaac
2020-12-01 14:18:33 -08:00
parent 0db60a3462
commit 1d00ff6dd8
3 changed files with 47 additions and 19 deletions

View File

@@ -33,26 +33,33 @@ func New(config ...Config) *Storage {
cfg := configDefault(config...)
// Create data source name
var dsn = "mongodb"
if cfg.Atlas {
dsn += "+srv://"
var dsn string
// Check if user supplied connection string
if cfg.Connection != "" {
dsn = cfg.Connection
} else {
dsn += "://"
}
if cfg.Username != "" {
dsn += url.QueryEscape(cfg.Username)
}
if cfg.Password != "" {
dsn += ":" + cfg.Password
}
if cfg.Username != "" || cfg.Password != "" {
dsn += "@"
}
// Cannot specify port when using MongoDB Atlas
if cfg.Atlas {
dsn += url.QueryEscape(cfg.Host)
} else {
dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port)
dsn = "mongodb"
if cfg.Atlas {
dsn += "+srv://"
} else {
dsn += "://"
}
if cfg.Username != "" {
dsn += url.QueryEscape(cfg.Username)
}
if cfg.Password != "" {
dsn += ":" + cfg.Password
}
if cfg.Username != "" || cfg.Password != "" {
dsn += "@"
}
// Cannot specify port when using MongoDB Atlas
if cfg.Atlas {
dsn += url.QueryEscape(cfg.Host)
} else {
dsn += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port)
}
}
// Set mongo options