metricator/cmd/metricatord/main.go

80 lines
1.7 KiB
Go
Raw Normal View History

2020-11-28 23:34:20 +05:00
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"go.dev.pztrn.name/metricator/internal/application"
2020-11-28 23:34:20 +05:00
"go.dev.pztrn.name/metricator/internal/common"
"go.dev.pztrn.name/metricator/internal/configuration"
"go.dev.pztrn.name/metricator/internal/httpserver"
2020-12-23 16:28:57 +05:00
"go.dev.pztrn.name/metricator/internal/logger"
2020-11-28 23:34:20 +05:00
)
func main() {
log.Printf("Starting Metricator daemon, version %s from branch %s (build #%s, commit hash %s)\n",
common.Version,
common.Branch,
common.Build,
common.CommitHash,
)
mainCtx, cancelFunc := context.WithCancel(context.Background())
config := configuration.NewConfig()
// Parse configuration.
2020-11-28 23:34:20 +05:00
flag.Parse()
2021-11-21 14:45:09 +05:00
if err := config.Parse(); err != nil {
2020-11-28 23:34:20 +05:00
log.Fatalln("Failed to parse configuration:", err.Error())
}
2020-11-28 23:34:20 +05:00
log.Printf("Configuration parsed: %+v\n", config)
// Create applications.
apps := make([]*application.Application, 0, len(config.Applications))
2020-12-23 16:28:57 +05:00
logger := logger.NewLogger(config.Logger)
httpSrv, httpStopped := httpserver.NewHTTPServer(mainCtx, config, logger)
for appName, appConfig := range config.Applications {
2020-12-23 16:28:57 +05:00
app := application.NewApplication(mainCtx, appName, appConfig, logger)
app.Start()
httpSrv.RegisterHandlerForApplication(appName, app.GetHandler())
apps = append(apps, app)
}
2020-11-28 23:34:20 +05:00
httpSrv.Start()
log.Println("Metricator is started and ready to serve requests")
// CTRL+C handler.
signalHandler := make(chan os.Signal, 1)
shutdownDone := make(chan bool, 1)
signal.Notify(signalHandler, os.Interrupt, syscall.SIGTERM)
go func(apps []*application.Application) {
2020-11-28 23:34:20 +05:00
<-signalHandler
cancelFunc()
for _, app := range apps {
<-app.GetDoneChan()
}
2020-11-28 23:34:20 +05:00
<-httpStopped
shutdownDone <- true
}(apps)
2020-11-28 23:34:20 +05:00
<-shutdownDone
log.Println("Metricator stopped")
os.Exit(0)
}