package main import ( "context" "flag" "os" "os/signal" "time" "git.faercol.me/faercol/http-boot-server/bootserver/config" "git.faercol.me/faercol/http-boot-server/bootserver/logger" "git.faercol.me/faercol/http-boot-server/bootserver/server" ) const stopTimeout = 10 * time.Second type cliArgs struct { configPath string } func parseArgs() *cliArgs { configPath := flag.String("config", "", "Path to the JSON configuration file") flag.Parse() return &cliArgs{ configPath: *configPath, } } func main() { args := parseArgs() mainCtx, cancel := context.WithCancel(context.Background()) conf, err := config.New(args.configPath) if err != nil { panic(err) } logger.Init(conf.LogLevel) logger.L.Infof("Initialized logger with level %v", conf.LogLevel) logger.L.Info("Initializing server") s, err := server.New(conf, logger.L) if err != nil { logger.L.Fatalf("Failed to initialize server: %s", err.Error()) } go s.Run(mainCtx) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) logger.L.Info("Application successfully started") logger.L.Debug("Waiting for stop signal") select { case <-s.Done(): logger.L.Fatal("Unexpected exit from server") case <-c: logger.L.Info("Stopping main application") cancel() } logger.L.Debugf("Waiting %v for all daemons to stop", stopTimeout) select { case <-time.After(stopTimeout): logger.L.Fatalf("Failed to stop all daemons in the expected time") case <-s.Done(): logger.L.Info("web server successfully stopped") } logger.L.Info("Application successfully stopped") os.Exit(0) }