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

@@ -48,11 +48,25 @@ store := mongodb.New(mongodb.Config{
Collection: "fiber_storage", Collection: "fiber_storage",
Reset: false, Reset: false,
}) })
// Initialize custom config using connection string
store := mongodb.New(mongodb.Config{
Connection: "mongodb://user:password@127.0.0.1:27017",
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
})
``` ```
### Config ### Config
```go ```go
type Config struct { type Config struct {
// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is ""
Connection string
// Whether the DB is hosted on MongoDB Atlas // Whether the DB is hosted on MongoDB Atlas
// //
// Optional. Default is false // Optional. Default is false
@@ -98,6 +112,7 @@ type Config struct {
### Default Config ### Default Config
```go ```go
var ConfigDefault = Config{ var ConfigDefault = Config{
Connection: "",
Atlas: false, Atlas: false,
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 27017, Port: 27017,

View File

@@ -2,6 +2,11 @@ package mongodb
// Config defines the config for storage. // Config defines the config for storage.
type Config struct { type Config struct {
// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is ""
Connection string
// Whether the DB is hosted on MongoDB Atlas // Whether the DB is hosted on MongoDB Atlas
// //
// Optional. Default is false // Optional. Default is false
@@ -45,6 +50,7 @@ type Config struct {
// ConfigDefault is the default config // ConfigDefault is the default config
var ConfigDefault = Config{ var ConfigDefault = Config{
Connection: "",
Atlas: false, Atlas: false,
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 27017, Port: 27017,

View File

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