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

@ -2,3 +2,8 @@
# you want to monitor here # 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" "log"
"net/http" "net/http"
"os" "os"
"strconv"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -18,8 +17,11 @@ func main() {
var ( var (
switchMac = os.Getenv("TPLINK_MAC") switchMac = os.Getenv("TPLINK_MAC")
port = 9717 port = os.Getenv("LISTEN_PORT")
) )
if port == "" {
port = "9717"
}
if switchMac == "" { if switchMac == "" {
log.Fatal("Env variable 'TPLINK_MAC' not set!") log.Fatal("Env variable 'TPLINK_MAC' not set!")
} }
@ -36,7 +38,7 @@ func main() {
// Start Web-Server // Start Web-Server
http.Handle("/metrics", promhttp.Handler()) http.Handle("/metrics", promhttp.Handler())
log.Printf("Listening on http://0.0.0.0:%d", port) log.Printf("Listening on http://0.0.0.0:%s", port)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil)) log.Fatal(http.ListenAndServe(":"+port, nil))
} }