diff --git a/.goreleaser.yml b/.goreleaser.yml index 5dcebf9..0157ef8 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 }}' diff --git a/main.go b/main.go index 65c8665..03d1049 100644 --- a/main.go +++ b/main.go @@ -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 }