Allow mgo.ParseURL to set [/database] (#997)

* Allow mgo.ParseURL to set [/database]

As per Michael's solution we need to be able to set the database from
either the parsed connection string, or fallback to athens if not set.

Signed-off-by: Chris M <me@christophermills.co.uk>

* Remove TimeoutDuration which was removed #928

Signed-off-by: Chris M <me@christophermills.co.uk>

* Correct conf_test to accommodate for changes

I'd missed out the addition of DefaultDBName from the test, this has now
been corrected

Signed-off-by: Chris M <me@christophermills.co.uk>

* Make sure gofmt runs automatically on this machine

Y'know simple stuff.

Signed-off-by: Chris M <me@christophermills.co.uk>
This commit is contained in:
Chris Mills
2019-02-14 18:21:34 +00:00
committed by Aaron Schlesinger
parent aa3fb3389c
commit 2cf10c3ead
4 changed files with 23 additions and 12 deletions
+5 -1
View File
@@ -189,10 +189,14 @@ StatsExporter = "prometheus"
Region = ""
[Storage.Mongo]
# Full URL for mongo storage
# Full connection string for mongo storage
# Env override: ATHENS_MONGO_STORAGE_URL
URL = "mongodb://127.0.0.1:27017"
# Sets default database name for mongo storage if not set by URL
# Env override: ATHENS_MONGO_DEFAULT_DATABASE
DefaultDBName = "athens"
# Path to certificate to use for the mongo connection
# Env override: ATHENS_MONGO_CERT_PATH
CertPath = ""
+3 -2
View File
@@ -121,8 +121,9 @@ func TestStorageEnvOverrides(t *testing.T) {
Region: "us-west-1",
},
Mongo: &MongoConfig{
URL: "mongoURL",
CertPath: "/test/path",
URL: "mongoURL",
CertPath: "/test/path",
DefaultDBName: "athens",
},
S3: &S3Config{
Region: "s3Region",
+4 -3
View File
@@ -2,7 +2,8 @@ package config
// MongoConfig specifies the properties required to use MongoDB as the storage backend
type MongoConfig struct {
URL string `validate:"required" envconfig:"ATHENS_MONGO_STORAGE_URL"`
CertPath string `envconfig:"ATHENS_MONGO_CERT_PATH"`
InsecureConn bool `envconfig:"ATHENS_MONGO_INSECURE"`
URL string `validate:"required" envconfig:"ATHENS_MONGO_STORAGE_URL"`
DefaultDBName string `envconfig:"ATHENS_MONGO_DEFAULT_DATABASE" default:"athens"`
CertPath string `envconfig:"ATHENS_MONGO_CERT_PATH"`
InsecureConn bool `envconfig:"ATHENS_MONGO_INSECURE"`
}
+11 -6
View File
@@ -34,7 +34,7 @@ func NewStorage(conf *config.MongoConfig, timeout time.Duration) (*ModuleStore,
}
ms := &ModuleStore{url: conf.URL, certPath: conf.CertPath, timeout: timeout}
err := ms.connect()
err := ms.connect(conf)
if err != nil {
return nil, errors.E(op, err)
}
@@ -42,11 +42,12 @@ func NewStorage(conf *config.MongoConfig, timeout time.Duration) (*ModuleStore,
return ms, nil
}
func (m *ModuleStore) connect() error {
func (m *ModuleStore) connect(conf *config.MongoConfig) error {
const op errors.Op = "mongo.connect"
var err error
m.s, err = m.newSession(m.timeout, m.insecure)
m.s, err = m.newSession(m.timeout, m.insecure, conf)
if err != nil {
return errors.E(op, err)
}
@@ -55,8 +56,6 @@ func (m *ModuleStore) connect() error {
}
func (m *ModuleStore) initDatabase() error {
// TODO: database and collection as env vars, or params to New()? together with user/mongo
m.d = "athens"
m.c = "modules"
index := mgo.Index{
@@ -69,7 +68,7 @@ func (m *ModuleStore) initDatabase() error {
return c.EnsureIndex(index)
}
func (m *ModuleStore) newSession(timeout time.Duration, insecure bool) (*mgo.Session, error) {
func (m *ModuleStore) newSession(timeout time.Duration, insecure bool, conf *config.MongoConfig) (*mgo.Session, error) {
tlsConfig := &tls.Config{}
dialInfo, err := mgo.ParseURL(m.url)
@@ -79,6 +78,12 @@ func (m *ModuleStore) newSession(timeout time.Duration, insecure bool) (*mgo.Ses
dialInfo.Timeout = timeout
if dialInfo.Database != "" {
m.d = dialInfo.Database
} else {
m.d = conf.DefaultDBName
}
if m.certPath != "" {
// Sets only when the env var is setup in config.dev.toml
tlsConfig.InsecureSkipVerify = insecure