2020-11-28 23:34:20 +05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
"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()
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
// Parse configuration.
|
2020-11-28 23:34:20 +05:00
|
|
|
flag.Parse()
|
2020-11-29 03:22:39 +05:00
|
|
|
|
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-29 03:22:39 +05:00
|
|
|
|
2020-11-28 23:34:20 +05:00
|
|
|
log.Printf("Configuration parsed: %+v\n", config)
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
// 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)
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
for appName, appConfig := range config.Applications {
|
2020-12-23 16:28:57 +05:00
|
|
|
app := application.NewApplication(mainCtx, appName, appConfig, logger)
|
2020-11-29 03:22:39 +05:00
|
|
|
app.Start()
|
|
|
|
|
2020-11-29 06:09:35 +05:00
|
|
|
httpSrv.RegisterHandlerForApplication(appName, app.GetHandler())
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
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)
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
go func(apps []*application.Application) {
|
2020-11-28 23:34:20 +05:00
|
|
|
<-signalHandler
|
|
|
|
cancelFunc()
|
|
|
|
|
2020-11-29 03:22:39 +05:00
|
|
|
for _, app := range apps {
|
|
|
|
<-app.GetDoneChan()
|
|
|
|
}
|
|
|
|
|
2020-11-28 23:34:20 +05:00
|
|
|
<-httpStopped
|
|
|
|
|
|
|
|
shutdownDone <- true
|
2020-11-29 03:22:39 +05:00
|
|
|
}(apps)
|
2020-11-28 23:34:20 +05:00
|
|
|
|
|
|
|
<-shutdownDone
|
|
|
|
log.Println("Metricator stopped")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|