Allow setting the listen port via an env variable

This commit is contained in:
2024-05-03 16:57:05 +02:00
parent 263fca5c10
commit 82445353b5
2 changed files with 12 additions and 5 deletions

View File

@ -1,4 +1,9 @@
# Enter the MAC Address of the TP-Link switch
# you want to monitor here
TPLINK_MAC=ff:ff:ff:ff:ff:ff
TPLINK_MAC=ff:ff:ff:ff:ff:ff
# The port to bind the Prometheus HTTP server on
# (default: 9717)
LISTEN_PORT=9717

10
main.go
View File

@ -4,7 +4,6 @@ import (
"log"
"net/http"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
@ -18,8 +17,11 @@ func main() {
var (
switchMac = os.Getenv("TPLINK_MAC")
port = 9717
port = os.Getenv("LISTEN_PORT")
)
if port == "" {
port = "9717"
}
if switchMac == "" {
log.Fatal("Env variable 'TPLINK_MAC' not set!")
}
@ -36,7 +38,7 @@ func main() {
// Start Web-Server
http.Handle("/metrics", promhttp.Handler())
log.Printf("Listening on http://0.0.0.0:%d", port)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
log.Printf("Listening on http://0.0.0.0:%s", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}