mirror of
https://github.com/TwiN/gatus.git
synced 2025-02-26 15:12:17 +01:00
Allow configuration file to be passed as parameter
This commit is contained in:
parent
66e48609b4
commit
c9c076a959
@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -15,28 +16,44 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNoServiceInConfig = errors.New("configuration file should contain at least 1 service")
|
ErrNoServiceInConfig = errors.New("configuration file should contain at least 1 service")
|
||||||
config *Config
|
ErrConfigFileNotFound = errors.New("configuration file not found")
|
||||||
|
ErrConfigNotLoaded = errors.New("configuration is nil")
|
||||||
|
config *Config
|
||||||
)
|
)
|
||||||
|
|
||||||
func Get() *Config {
|
func Get() *Config {
|
||||||
if config == nil {
|
if config == nil {
|
||||||
cfg, err := readConfigurationFile("config.yaml")
|
panic(ErrConfigNotLoaded)
|
||||||
if err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
cfg, err = readConfigurationFile("config.yml")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config = cfg
|
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Load(configFile string) error {
|
||||||
|
log.Printf("[config][Load] Attempting to load config from configFile=%s", configFile)
|
||||||
|
cfg, err := readConfigurationFile(configFile)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return ErrConfigFileNotFound
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
config = cfg
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadDefaultConfiguration() error {
|
||||||
|
err := Load("config.yaml")
|
||||||
|
if err != nil {
|
||||||
|
if err == ErrConfigFileNotFound {
|
||||||
|
return Load("config.yml")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func readConfigurationFile(fileName string) (config *Config, err error) {
|
func readConfigurationFile(fileName string) (config *Config, err error) {
|
||||||
var bytes []byte
|
var bytes []byte
|
||||||
if bytes, err = ioutil.ReadFile(fileName); err == nil {
|
if bytes, err = ioutil.ReadFile(fileName); err == nil {
|
||||||
|
20
main.go
20
main.go
@ -8,20 +8,36 @@ import (
|
|||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go watchdog.Monitor()
|
cfg := loadConfiguration()
|
||||||
|
go watchdog.Monitor(cfg)
|
||||||
http.HandleFunc("/api/v1/results", serviceResultsHandler)
|
http.HandleFunc("/api/v1/results", serviceResultsHandler)
|
||||||
http.HandleFunc("/health", healthHandler)
|
http.HandleFunc("/health", healthHandler)
|
||||||
http.Handle("/", http.FileServer(http.Dir("./static")))
|
http.Handle("/", http.FileServer(http.Dir("./static")))
|
||||||
if config.Get().Metrics {
|
if cfg.Metrics {
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
}
|
}
|
||||||
log.Println("[main][main] Listening on port 8080")
|
log.Println("[main][main] Listening on port 8080")
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadConfiguration() *config.Config {
|
||||||
|
args := os.Args
|
||||||
|
var err error
|
||||||
|
if len(args) == 2 {
|
||||||
|
err = config.Load(args[1])
|
||||||
|
} else {
|
||||||
|
err = config.LoadDefaultConfiguration()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return config.Get()
|
||||||
|
}
|
||||||
|
|
||||||
func serviceResultsHandler(writer http.ResponseWriter, _ *http.Request) {
|
func serviceResultsHandler(writer http.ResponseWriter, _ *http.Request) {
|
||||||
serviceResults := watchdog.GetServiceResults()
|
serviceResults := watchdog.GetServiceResults()
|
||||||
writer.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
|
@ -18,8 +18,8 @@ func GetServiceResults() *map[string][]*core.Result {
|
|||||||
return &serviceResults
|
return &serviceResults
|
||||||
}
|
}
|
||||||
|
|
||||||
func Monitor() {
|
func Monitor(cfg *config.Config) {
|
||||||
for _, service := range config.Get().Services {
|
for _, service := range cfg.Services {
|
||||||
go func(service *core.Service) {
|
go func(service *core.Service) {
|
||||||
for {
|
for {
|
||||||
log.Printf("[watchdog][Monitor] Monitoring serviceName=%s", service.Name)
|
log.Printf("[watchdog][Monitor] Monitoring serviceName=%s", service.Name)
|
||||||
|
Loading…
Reference in New Issue
Block a user