add environment variable configuration
This commit is contained in:
parent
c4c38f0dc9
commit
1436c9df32
5 changed files with 94 additions and 18 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -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
|
|
@ -1 +1,6 @@
|
|||
# weather-api
|
||||
# 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.
|
55
config/config.go
Normal file
55
config/config.go
Normal file
|
@ -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
|
||||
}
|
32
main.go
32
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()
|
||||
|
||||
}
|
||||
|
|
15
run_default.ps1
Normal file
15
run_default.ps1
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue