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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
httpSrv, httpStopped := httpserver.NewHTTPServer(mainCtx, config)
|
|
|
|
|
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
|
|
|
|
2020-11-28 23:34:20 +05:00
|
|
|
err := config.Parse()
|
|
|
|
if err != nil {
|
|
|
|
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))
|
|
|
|
|
|
|
|
for appName, appConfig := range config.Applications {
|
|
|
|
app := application.NewApplication(mainCtx, appName, appConfig)
|
|
|
|
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)
|
|
|
|
}
|