From 1436c9df3242ec79f079e09fe7ba364eab7f3aea Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Wed, 17 Mar 2021 20:43:20 +0100 Subject: [PATCH] add environment variable configuration --- .gitignore | 3 +++ README.md | 7 +++++- config/config.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 32 +++++++++++++--------------- run_default.ps1 | 15 +++++++++++++ 5 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 config/config.go create mode 100644 run_default.ps1 diff --git a/.gitignore b/.gitignore index 66fd13c..d0ada61 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +#run-script including environment-variable values for test instances +run.ps1 \ No newline at end of file diff --git a/README.md b/README.md index 3756b9d..33495ad 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ -# weather-api \ No newline at end of file +# weather-api + +## Applikation ausführen + +Eine lokal ausgeführte Test-Instanz der Wetter-API muss mit URLs, Tokens und ähnlichem über Umgebungsvariablen konfiguriert werden. +Das PowerShell-Skript `run_default.ps1` ist eine Vorlage für den start einer eigenen Instanz, lediglich die Umgebungsvariablen müsssen hierzu angepasst werden. Am besten wird der Inhalt dieses Skriptes in ein weiteres Skript (z.B. `run.ps1`) kopiert. Dieses wird von Git ignoriert, geheime Zugangsdaten (z.B. zu MQTT Broker, InfluxDB) werden so nicht ins Git-Repository eingefügt. \ No newline at end of file diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..64ebf8a --- /dev/null +++ b/config/config.go @@ -0,0 +1,55 @@ +package config + +import ( + "os" +) + +// const influx stuff +const influxToken = "default-token" +const influxWeatherBucket = "default-bucket" +const influxOrganization = "default-org" +const influxURL = "https://influx.default-address.com" + +//const mqtt stuff +const mqttURL = "tcp://default-address.com:1883" +const mqttTopic = "sensor/#" +const defaultLocation = "default-location" + +//influx config +func GetInfluxUrl() string { + return getVariableWithDefault("WEATHER-API-INFLUX_URL", influxURL) +} + +func GetInfluxToken() string { + return getVariableWithDefault("WEATHER-API-INFLUX_TOKEN", influxToken) +} + +func GetInfluxOrganization() string { + return getVariableWithDefault("WEATHER-API-INFLUX_ORG", influxOrganization) +} + +func GetInfluxBucket() string { + return getVariableWithDefault("WEATHER-API-INFLUX_BUCKET", influxWeatherBucket) +} + +//mqtt config +func GetMqttUrl() string { + return getVariableWithDefault("WEATHER-API-MQTT_URL", mqttURL) +} + +func GetMqttTopic() string { + return getVariableWithDefault("WEATHER-API-MQTT_TOPIC", mqttTopic) +} + +func GetMqttLocation() string { + return getVariableWithDefault("WEATHER-API-MQTT_LOCATION", defaultLocation) +} + +//helper +func getVariableWithDefault(variableKey, defaultValue string) string { + variable := os.Getenv(variableKey) + if len(variable) == 0 { + return defaultValue + } + return variable +} diff --git a/main.go b/main.go index a7606e6..df53e9c 100644 --- a/main.go +++ b/main.go @@ -3,28 +3,20 @@ package main import ( "os" "weather-data/api" + "weather-data/config" "weather-data/storage" "weather-data/weathersource" ) -// const influx stuff -const influxToken = "Pg34RXv4QE488ayCeY6JX4p3EwcoNhLu-zPQDn9zxirFmc0og9DCgamf02jrVEAN9mS4mT05nprGUkSrKQAUjA==" -const influxWeatherBucket = "weatherdata" -const influxOrganization = "weather-org" -const influxURL = "https://influx.gamlo-cloud.de" - -//const mqtt stuff -const mqttURL = "tcp://gamlo-cloud.de:1883" -const mqttTopic = "sensor/#" -const defaultLocation = "default location" - -//const api stuff -const apiAddress = ":10000" - func main() { //setup a new weatherstorage -> InfluxDB var weatherStorage storage.WeatherStorage - weatherStorage, err := storage.NewInfluxStorage(influxToken, influxWeatherBucket, influxOrganization, influxURL) + weatherStorage, err := storage.NewInfluxStorage( + config.GetInfluxToken(), + config.GetInfluxBucket(), + config.GetInfluxOrganization(), + config.GetInfluxUrl()) + if err != nil { os.Exit(1) } @@ -37,20 +29,26 @@ func main() { //add a new weatherData source -> mqtt var weatherSource weathersource.WeatherSource - weatherSource, err = weathersource.NewMqttSource(mqttURL, mqttTopic, defaultLocation) + weatherSource, err = weathersource.NewMqttSource( + config.GetMqttUrl(), + config.GetMqttTopic(), + config.GetMqttLocation()) + if err != nil { os.Exit(1) } defer weatherSource.Close() + weatherSource.AddNewWeatherDataCallback(newWeatherDataHandler) //setup a API -> REST var weatherAPI api.WeatherAPI - weatherAPI = api.NewRestAPI(apiAddress, weatherStorage) + weatherAPI = api.NewRestAPI(":10000", weatherStorage) err = weatherAPI.Start() if err != nil { os.Exit(1) } defer weatherAPI.Close() + } diff --git a/run_default.ps1 b/run_default.ps1 new file mode 100644 index 0000000..866a815 --- /dev/null +++ b/run_default.ps1 @@ -0,0 +1,15 @@ +#build the application +go build main.go + +#set environment variables for weather-api configuration +Set-Item -Path "Env:WEATHER-API-INFLUX_URL" -Value "https://influx.default-address.com" +Set-Item -Path "Env:WEATHER-API-INFLUX_TOKEN" -Value "default-token" +Set-Item -Path "Env:WEATHER-API-INFLUX_ORG" -Value "default-org" +Set-Item -Path "Env:WEATHER-API-INFLUX_BUCKET" -Value "default-bucket" + +Set-Item -Path "Env:WEATHER-API-MQTT_URL" -Value "tcp://default-address.com:1883" +Set-Item -Path "Env:WEATHER-API-MQTT_TOPIC" -Value "sensor/#" +Set-Item -Path "Env:WEATHER-API-MQTT_LOCATION" -Value "default-location" + +#start application +Start-Process "main.exe" -Wait -NoNewWindow