refactor(db): unify env getters
Signed-off-by: Klagarge <remi@heredero.ch>
This commit is contained in:
@@ -27,20 +27,10 @@ type ProvenceData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mqttConnection() *mqtt.MqttGateway {
|
func mqttConnection() *mqtt.MqttGateway {
|
||||||
BrokerUrl, ok := os.LookupEnv("MQTT_BROKER_URL")
|
BrokerUrl := getEnv("MQTT_BROKER_URL", "tls://localhost:8883")
|
||||||
if !ok {
|
Username := getEnv("MQTT_USERNAME", "user")
|
||||||
BrokerUrl = "tls://localhost:8883"
|
Password := getEnv("MQTT_PASSWORD", "password")
|
||||||
}
|
|
||||||
|
|
||||||
Username, ok := os.LookupEnv("MQTT_USERNAME")
|
|
||||||
if !ok {
|
|
||||||
Username = "user"
|
|
||||||
}
|
|
||||||
|
|
||||||
Password, ok := os.LookupEnv("MQTT_PASSWORD")
|
|
||||||
if !ok {
|
|
||||||
Password = "password"
|
|
||||||
}
|
|
||||||
ClientId := "mqtt-gateway-test-client_" + fmt.Sprint(time.Now().Unix())
|
ClientId := "mqtt-gateway-test-client_" + fmt.Sprint(time.Now().Unix())
|
||||||
|
|
||||||
// Create config & gateway
|
// Create config & gateway
|
||||||
@@ -64,18 +54,12 @@ func mqttConnection() *mqtt.MqttGateway {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func influxConnection() *influx.InfluxGateway {
|
func influxConnection() *influx.InfluxGateway {
|
||||||
influxUrl, ok := os.LookupEnv("INFLUX_URL")
|
influxUrl := getEnv("INFLUX_URL", "https://db.e.kb28.ch:443")
|
||||||
if !ok {
|
|
||||||
influxUrl = "https://db.e.kb28.ch:443"
|
|
||||||
}
|
|
||||||
influxDatabase, ok := os.LookupEnv("INFLUX_DATABASE")
|
|
||||||
if !ok {
|
|
||||||
influxDatabase = "provence"
|
|
||||||
}
|
|
||||||
|
|
||||||
influxToken := os.Getenv("INFLUX_TOKEN")
|
influxDatabase := getEnv("INFLUX_DATABASE", "provence")
|
||||||
|
influxToken := getEnv("INFLUX_TOKEN", "")
|
||||||
if influxToken == "" {
|
if influxToken == "" {
|
||||||
if tokenFile := os.Getenv("INFLUX_TOKEN_FILE"); tokenFile != "" {
|
if tokenFile := getEnv("INFLUX_TOKEN_FILE", "/run/secrets/admin-token"); tokenFile != "" {
|
||||||
content, err := os.ReadFile(tokenFile)
|
content, err := os.ReadFile(tokenFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
influxToken = strings.TrimSpace(string(content))
|
influxToken = strings.TrimSpace(string(content))
|
||||||
@@ -108,10 +92,7 @@ func influxConnection() *influx.InfluxGateway {
|
|||||||
// @securityDefinitions.basic BasicAuth
|
// @securityDefinitions.basic BasicAuth
|
||||||
func main() {
|
func main() {
|
||||||
// Load mapping configuration
|
// Load mapping configuration
|
||||||
mappingPath := os.Getenv("MAPPING_CONFIG_PATH")
|
mappingPath := getEnv("MAPPING_CONFIG_PATH", "mapping.json")
|
||||||
if mappingPath == "" {
|
|
||||||
mappingPath = "mapping.json"
|
|
||||||
}
|
|
||||||
mapping, err := LoadMapping(mappingPath)
|
mapping, err := LoadMapping(mappingPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Main] Warning: could not load mapping file: %v. Using defaults.", err)
|
log.Printf("[Main] Warning: could not load mapping file: %v. Using defaults.", err)
|
||||||
@@ -124,10 +105,7 @@ func main() {
|
|||||||
influxGateway := influxConnection()
|
influxGateway := influxConnection()
|
||||||
defer influxGateway.Close()
|
defer influxGateway.Close()
|
||||||
|
|
||||||
measurementName := os.Getenv("CAMPUS")
|
measurementName := getEnv("CAMPUS", "provence")
|
||||||
if measurementName == "" {
|
|
||||||
measurementName = "provence"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create measurement for provence topic
|
// Create measurement for provence topic
|
||||||
provenceMeasurement := point.CreateMeasurement[ProvenceData](measurementName)
|
provenceMeasurement := point.CreateMeasurement[ProvenceData](measurementName)
|
||||||
@@ -177,10 +155,7 @@ func main() {
|
|||||||
restPassword := getEnv("REST_PASSWORD", "password")
|
restPassword := getEnv("REST_PASSWORD", "password")
|
||||||
restGateway := rest.NewRestGateway(influxGateway, measurementName, restUsername, restPassword)
|
restGateway := rest.NewRestGateway(influxGateway, measurementName, restUsername, restPassword)
|
||||||
|
|
||||||
port, ok := os.LookupEnv("REST_PORT")
|
port := getEnv("REST_PORT", "8080")
|
||||||
if !ok {
|
|
||||||
port = "8080"
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("[Main] Starting REST Gateway on port %s\n", port)
|
log.Printf("[Main] Starting REST Gateway on port %s\n", port)
|
||||||
if err := restGateway.Run(":" + port); err != nil {
|
if err := restGateway.Run(":" + port); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user