Skip to content

Commit

Permalink
Fix crash when starting the container image
Browse files Browse the repository at this point in the history
The binary does not know what it needs to listen.
  • Loading branch information
LeSuisse committed Jun 18, 2023
1 parent 334e802 commit afdcfa9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ kos:
tags:
- '{{.Version}}'
- latest
ldflags:
- -s -w -X main.DefaultAddressMetrics=0.0.0.0:17795 -X main.DefaultAddressSensor=0.0.0.0:17796
bare: true
creation_time: '{{ .CommitTimestamp }}'
ko_data_creation_time: '{{ .CommitTimestamp }}'
Expand Down
19 changes: 13 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,34 @@ import (

const SensorName = "name"

var DefaultAddressMetrics = ""
var DefaultAddressSensor = ""

func main() {
gin.SetMode(gin.ReleaseMode)

routerSensor := gin.Default()
routerSensor.Use(gin.BasicAuth(gin.Accounts{"sensor": getEnv("SENSOR_PASSWORD")}))
routerSensor.Use(gin.BasicAuth(gin.Accounts{"sensor": getEnv("SENSOR_PASSWORD", "")}))
routerSensor.GET("/sensors/:sensor_name", sensorAction)

routerMetrics := gin.Default()
routerMetrics.GET("/metrics", promHandler(promhttp.Handler()))
go func() {
log.Fatal(routerMetrics.Run(getEnv("ADDRESS_METRICS")))
log.Fatal(routerMetrics.Run(getEnv("ADDRESS_METRICS", DefaultAddressMetrics)))
}()

log.Fatal(routerSensor.Run(getEnv("ADDRESS_SENSOR")))
log.Fatal(routerSensor.Run(getEnv("ADDRESS_SENSOR", DefaultAddressSensor)))
}

func getEnv(name string) string {
func getEnv(name string, defaultValue string) string {
value, exist := os.LookupEnv(name)
if !exist {
log.Fatalf("%s environment variable is missing\n", name)
if exist {
return value
}
if defaultValue != "" {
return defaultValue
}
log.Fatalf("%s environment variable is missing\n", name)
return value
}

Expand Down

0 comments on commit afdcfa9

Please sign in to comment.