From 1d00ff6dd80f530bdb001baf1f1a97579c9f4873 Mon Sep 17 00:00:00 2001 From: Kalissaac Date: Tue, 1 Dec 2020 14:18:33 -0800 Subject: [PATCH 1/3] Add ability to supply Mongo connection uri --- mongodb/README.md | 15 +++++++++++++++ mongodb/config.go | 6 ++++++ mongodb/mongodb.go | 45 ++++++++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/mongodb/README.md b/mongodb/README.md index 6f7f500d..8ba261c7 100644 --- a/mongodb/README.md +++ b/mongodb/README.md @@ -48,11 +48,25 @@ store := mongodb.New(mongodb.Config{ Collection: "fiber_storage", 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 ```go 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 // // Optional. Default is false @@ -98,6 +112,7 @@ type Config struct { ### Default Config ```go var ConfigDefault = Config{ + Connection: "", Atlas: false, Host: "127.0.0.1", Port: 27017, diff --git a/mongodb/config.go b/mongodb/config.go index bcc1fed8..298ac8f0 100644 --- a/mongodb/config.go +++ b/mongodb/config.go @@ -2,6 +2,11 @@ package mongodb // Config defines the config for storage. 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 // // Optional. Default is false @@ -45,6 +50,7 @@ type Config struct { // ConfigDefault is the default config var ConfigDefault = Config{ + Connection: "", Atlas: false, Host: "127.0.0.1", Port: 27017, diff --git a/mongodb/mongodb.go b/mongodb/mongodb.go index 95797d10..2af77c88 100644 --- a/mongodb/mongodb.go +++ b/mongodb/mongodb.go @@ -33,26 +33,33 @@ func New(config ...Config) *Storage { cfg := configDefault(config...) // Create data source name - var 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) + var dsn string + + // Check if user supplied connection string + if cfg.Connection != "" { + dsn = cfg.Connection } 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 From e5d9b064caf2924c9a67dd3cca713ce6bc9157ea Mon Sep 17 00:00:00 2001 From: Kalissaac Date: Tue, 1 Dec 2020 15:01:06 -0800 Subject: [PATCH 2/3] Remove Atlas config option --- mongodb/README.md | 7 ------- mongodb/config.go | 6 ------ mongodb/mongodb.go | 14 ++------------ 3 files changed, 2 insertions(+), 25 deletions(-) diff --git a/mongodb/README.md b/mongodb/README.md index 8ba261c7..8ea75589 100644 --- a/mongodb/README.md +++ b/mongodb/README.md @@ -41,7 +41,6 @@ store := mongodb.New() // Initialize custom config store := mongodb.New(mongodb.Config{ - Atlas: false, Host: "127.0.0.1", Port: 27017, Database: "fiber", @@ -67,11 +66,6 @@ type Config struct { // Optional. Default is "" Connection string - // Whether the DB is hosted on MongoDB Atlas - // - // Optional. Default is false - Atlas bool - // Host name where the DB is hosted // // Optional. Default is "127.0.0.1" @@ -113,7 +107,6 @@ type Config struct { ```go var ConfigDefault = Config{ Connection: "", - Atlas: false, Host: "127.0.0.1", Port: 27017, Database: "fiber", diff --git a/mongodb/config.go b/mongodb/config.go index 298ac8f0..7c11bda4 100644 --- a/mongodb/config.go +++ b/mongodb/config.go @@ -7,11 +7,6 @@ type Config struct { // Optional. Default is "" Connection string - // Whether the DB is hosted on MongoDB Atlas - // - // Optional. Default is false - Atlas bool - // Host name where the DB is hosted // // Optional. Default is "127.0.0.1" @@ -51,7 +46,6 @@ type Config struct { // ConfigDefault is the default config var ConfigDefault = Config{ Connection: "", - Atlas: false, Host: "127.0.0.1", Port: 27017, Database: "fiber", diff --git a/mongodb/mongodb.go b/mongodb/mongodb.go index 2af77c88..fc9c05cb 100644 --- a/mongodb/mongodb.go +++ b/mongodb/mongodb.go @@ -39,12 +39,7 @@ func New(config ...Config) *Storage { if cfg.Connection != "" { dsn = cfg.Connection } else { - dsn = "mongodb" - if cfg.Atlas { - dsn += "+srv://" - } else { - dsn += "://" - } + dsn = "mongodb://" if cfg.Username != "" { dsn += url.QueryEscape(cfg.Username) } @@ -54,12 +49,7 @@ func New(config ...Config) *Storage { 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 += fmt.Sprintf("%s:%d", url.QueryEscape(cfg.Host), cfg.Port) } // Set mongo options From ea23fabafab7d6f5bb2e2a758d3d746594bc9e2c Mon Sep 17 00:00:00 2001 From: Kalissaac Date: Tue, 1 Dec 2020 16:12:51 -0800 Subject: [PATCH 3/3] Rename Connection to ConnectionURL --- mongodb/README.md | 22 +++++++++++----------- mongodb/config.go | 14 +++++++------- mongodb/mongodb.go | 4 ++-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/mongodb/README.md b/mongodb/README.md index 8ea75589..5322bb20 100644 --- a/mongodb/README.md +++ b/mongodb/README.md @@ -50,10 +50,10 @@ store := mongodb.New(mongodb.Config{ // 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, + ConnectionURI: "mongodb://user:password@127.0.0.1:27017", + Database: "fiber", + Collection: "fiber_storage", + Reset: false, }) ``` @@ -64,7 +64,7 @@ type Config struct { // Connection string to use for DB. Will override all other authentication values if used // // Optional. Default is "" - Connection string + ConnectionURI string // Host name where the DB is hosted // @@ -106,11 +106,11 @@ type Config struct { ### Default Config ```go var ConfigDefault = Config{ - Connection: "", - Host: "127.0.0.1", - Port: 27017, - Database: "fiber", - Collection: "fiber_storage", - Reset: false, + ConnectionURI: "", + Host: "127.0.0.1", + Port: 27017, + Database: "fiber", + Collection: "fiber_storage", + Reset: false, } ``` diff --git a/mongodb/config.go b/mongodb/config.go index 7c11bda4..7e496bc8 100644 --- a/mongodb/config.go +++ b/mongodb/config.go @@ -5,7 +5,7 @@ type Config struct { // Connection string to use for DB. Will override all other authentication values if used // // Optional. Default is "" - Connection string + ConnectionURI string // Host name where the DB is hosted // @@ -45,12 +45,12 @@ type Config struct { // ConfigDefault is the default config var ConfigDefault = Config{ - Connection: "", - Host: "127.0.0.1", - Port: 27017, - Database: "fiber", - Collection: "fiber_storage", - Reset: false, + ConnectionURI: "", + Host: "127.0.0.1", + Port: 27017, + Database: "fiber", + Collection: "fiber_storage", + Reset: false, } // Helper function to set default values diff --git a/mongodb/mongodb.go b/mongodb/mongodb.go index fc9c05cb..b33c9974 100644 --- a/mongodb/mongodb.go +++ b/mongodb/mongodb.go @@ -36,8 +36,8 @@ func New(config ...Config) *Storage { var dsn string // Check if user supplied connection string - if cfg.Connection != "" { - dsn = cfg.Connection + if cfg.ConnectionURI != "" { + dsn = cfg.ConnectionURI } else { dsn = "mongodb://" if cfg.Username != "" {