diff --git a/config/config.go b/config/config.go index 0e45d2e..355932c 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,8 @@ const mqttMinDistToLastValue = 250 * time.Millisecond const mongodbURL = "mongodb://default-address.com:27017" const mongodbName = "weathersensors" const mongodbCollection = "sensordata" +const mongodbUser = "mongoUser" +const mongdbPassword = "mongoPassword" //other config stuff const allowUnregisteredSensors = false @@ -97,6 +99,14 @@ func GetMongodbCollection() string { return getVariableWithDefault("WEATHER-API-MONGODB_COLLECTION", mongodbCollection) } +func GetMongodbUserName() string { + return getVariableWithDefault("WEATHER-API-MONGODB_USER", mongodbUser) +} + +func GetMongodbPassword() string { + return getVariableWithDefault("WEATHER-API-MONGODB_PASSWORD", mongdbPassword) +} + //common config func AllowUnregisteredSensors() bool { return getVariableWithDefaultBool("WEATHER-API-ALLOW_UNREGISTERED_SENSORS", allowUnregisteredSensors) diff --git a/main.go b/main.go index f4e6c03..861637f 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,9 @@ func main() { sensorRegistry, err = storage.NewMongodbSensorRegistry( config.GetMongodbURL(), config.GetMongodbName(), - config.GetMongodbCollection()) + config.GetMongodbCollection(), + config.GetMongodbUserName(), + config.GetMongodbPassword()) if err != nil { os.Exit(1) diff --git a/run_default.ps1 b/run_default.ps1 index e8c64a1..18f0f55 100644 --- a/run_default.ps1 +++ b/run_default.ps1 @@ -17,6 +17,8 @@ Set-Item -Path "Env:WEATHER-API-MQTT_MIN_DIST_TO_LAST_VALUE" -Value "250" Set-Item -Path "Env:WEATHER-API-MONGODB_URL" -Value "mongodb://default-address.com:27017" Set-Item -Path "Env:WEATHER-API-MONGODB_NAME" -Value "weathersensors" Set-Item -Path "Env:WEATHER-API-MONGODB_COLLECTION" -Value "sensordata" +Set-Item -Path "Env:WEATHER-API-MONGODB_USER" -Value "mongoUser" +Set-Item -Path "Env:WEATHER-API-MONGODB_PASSWORD" -Value "mongoPassword" Set-Item -Path "Env:WEATHER-API-ANONYMOUS_MQTT_AUTHENTICATION" -Value "false" Set-Item -Path "Env:WEATHER-API-ALLOW_UNREGISTERED_SENSORS" -Value "true" diff --git a/storage/mongodb-storage.go b/storage/mongodb-storage.go index 8c1b28d..3851531 100644 --- a/storage/mongodb-storage.go +++ b/storage/mongodb-storage.go @@ -19,10 +19,12 @@ type mongodbSensorRegistry struct { client *mongo.Client } -func NewMongodbSensorRegistry(connection, database, collection string) (*mongodbSensorRegistry, error) { +func NewMongodbSensorRegistry(connection, database, collection, user, password string) (*mongodbSensorRegistry, error) { sensorRegistry := new(mongodbSensorRegistry) - client, err := mongo.NewClient(options.Client().ApplyURI(connection)) + options := options.Client().ApplyURI(connection).SetAuth(options.Credential{Username: user, Password: password}) + + client, err := mongo.NewClient(options) if err != nil { return nil, err }