package main import ( "context" "errors" "log" "net/http" "os" "os/signal" "syscall" "github.com/prometheus/client_golang/prometheus/promhttp" "ocspcrl/internal/metrics" ) func startServer(server *http.Server, label string) <-chan struct{} { closed := make(chan struct{}) go func() { log.Printf("starting %s server on %+q", label, server.Addr) if listenError := server.ListenAndServe(); !errors.Is(listenError, http.ErrServerClosed) { log.Printf("%s server error: %v", label, listenError) } close(closed) }() return closed } func main() { log.SetFlags(log.Lshortfile) config := parseConfiguration(os.Args[1:]) cas, discoverError := discoverCas(config.casDirectory) if discoverError != nil { log.Fatalf("failed to load cas: %v", discoverError) } applicationRouter := buildApplicationRouter(cas) terminationChan := make(chan os.Signal, 1) signal.Notify(terminationChan, syscall.SIGINT, syscall.SIGTERM) hupChan := make(chan os.Signal, 1) signal.Notify(hupChan, syscall.SIGHUP) go runReloadWorker(hupChan, cas) applicationServer := &http.Server{Addr: config.applicationListenAddress, Handler: metrics.Middleware(applicationRouter)} metricsServer := &http.Server{Addr: config.metricsListenAddress, Handler: promhttp.Handler()} applicationServerClosed := startServer(applicationServer, "application") metricsServerClosed := startServer(metricsServer, "metrics") <-terminationChan close(hupChan) applicationServer.Shutdown(context.TODO()) metricsServer.Shutdown(context.TODO()) <-applicationServerClosed <-metricsServerClosed }