nurse/main.go

93 lines
2 KiB
Go
Raw Normal View History

2022-04-28 16:35:02 +00:00
package main
import (
2022-05-13 13:38:19 +00:00
"errors"
"log"
2022-05-13 13:38:19 +00:00
"net/http"
2022-04-28 16:35:02 +00:00
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
2022-05-13 13:38:19 +00:00
"github.com/baez90/nurse/api"
"github.com/baez90/nurse/check"
"github.com/baez90/nurse/config"
2022-05-13 13:38:19 +00:00
"github.com/baez90/nurse/protocols/redis"
2022-04-28 16:35:02 +00:00
)
var (
logLevel = zapcore.InfoLevel
cfgFile string
cfg config.Nurse
)
2022-04-28 16:35:02 +00:00
func main() {
if err := prepareFlags(); err != nil {
log.Fatalf("Failed to parse flags: %v", err)
}
setupLogging()
logger := zap.L()
2022-05-13 13:38:19 +00:00
nurseInstance, err := config.New(
config.WithValuesFrom(cfg),
config.WithConfigFile(cfgFile),
config.WithServersFromEnv(),
config.WithEndpointsFromEnv(),
)
if err != nil {
2022-05-13 13:38:19 +00:00
logger.Fatal("Failed to load config from environment", zap.Error(err))
2022-04-28 16:35:02 +00:00
}
2022-05-13 13:38:19 +00:00
logger.Debug("Loaded config", zap.Any("config", nurseInstance))
chkRegistry := check.NewRegistry()
2022-06-09 20:12:45 +00:00
if err = chkRegistry.Register(redis.Module()); err != nil {
2022-05-13 13:38:19 +00:00
logger.Fatal("Failed to register Redis module", zap.Error(err))
}
srvLookup, err := nurseInstance.ServerLookup()
if err != nil {
logger.Fatal("Failed to prepare server lookup", zap.Error(err))
}
mux, err := api.PrepareMux(nurseInstance, chkRegistry, srvLookup)
if err != nil {
logger.Fatal("Failed to prepare server mux", zap.Error(err))
}
if err := http.ListenAndServe(":8080", mux); err != nil {
if errors.Is(err, http.ErrServerClosed) {
return
}
logger.Fatal("Failed to serve HTTP", zap.Error(err))
}
}
func setupLogging() {
cfg := zap.NewProductionConfig()
cfg.Level = zap.NewAtomicLevelAt(logLevel)
logger, err := cfg.Build()
if err != nil {
log.Fatalf("Failed to setup logging: %v", err)
2022-04-28 16:35:02 +00:00
}
zap.ReplaceGlobals(logger)
2022-04-28 16:35:02 +00:00
}
func prepareFlags() error {
set := config.ConfigureFlags(&cfg)
set.Var(&logLevel, "log-level", "Log level to use")
set.StringVar(
&cfgFile,
"config",
config.LookupEnvOr[string]("NURSE_CONFIG", "", config.Identity[string]),
"Config file to load, if not set $HOME/.nurse.yaml, /etc/nurse/config.yaml and ./nurse.yaml are tried - optional",
)
return set.Parse(os.Args)
2022-04-28 16:35:02 +00:00
}