Add configuration values for address and port

Add (non-mandatory) configuration values to set address and port on which the web frontent will be served. If not set defaults will be applied.
This commit is contained in:
Michael Engelhardt
2020-11-19 19:39:48 +01:00
parent 9f485b14e0
commit 10310cf380
5 changed files with 210 additions and 34 deletions

35
main.go
View File

@ -8,7 +8,6 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
@ -24,38 +23,8 @@ var (
cachedServiceResults []byte
cachedServiceResultsGzipped []byte
cachedServiceResultsTimestamp time.Time
port int
host string
)
func init() {
// Customizing priority:
// (1) command line parameters will be preferred over
// (2) environment variables will be preferred over
// (3) application defaults
// set defaults for the case that neither an environment variable nor a
// command line parameter is passed
var defaultHost = ""
var defaultPort = 8080
// assume set if the is a valid port number
if p, err := strconv.Atoi(os.Getenv("GATUS_CONFIG_PORT")); err == nil && p > 0 {
defaultPort = p
}
// explicitly asked if the user has set a the environment variable to
// blank / empty in order to allow listening on all interfaces
if h, set := os.LookupEnv("GATUS_CONFIG_HOST"); set == true {
defaultHost = h
}
flag.IntVar(&port, "port", defaultPort, "port to listen (default: 8080)")
flag.IntVar(&port, "p", defaultPort, "port to listen (default: 8080 ; shorthand)")
flag.StringVar(&host, "host", defaultHost, "host to listen on (default all interfaces on host)")
flag.StringVar(&host, "h", defaultHost, "host to listen on (default all interfaces on host; shorthand)")
}
func main() {
flag.Parse()
@ -71,9 +40,9 @@ func main() {
http.Handle("/metrics", promhttp.Handler())
}
log.Printf("[main][main] Listening on %s:%d", host, port)
log.Printf("[main][main] Listening on %s:%d\r\n", cfg.Web.Address, cfg.Web.Port)
go watchdog.Monitor(cfg)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil))
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Web.Address, cfg.Web.Port), nil))
}
func loadConfiguration() *config.Config {