mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
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:
committed by
Aaron Schlesinger
parent
aa3fb3389c
commit
2cf10c3ead
+5
-1
@@ -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 = ""
|
||||
|
||||
@@ -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
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user