Added graceful shutdown.

This commit is contained in:
Marvin Blum
2020-06-24 15:05:13 +02:00
committed by Marvin Blum
parent 4515f3d165
commit e5eee70815
2 changed files with 22 additions and 13 deletions

33
main.go
View File

@@ -1,11 +1,11 @@
package main package main
import ( import (
"context"
"github.com/Kugelschieber/marvinblum.de/blog" "github.com/Kugelschieber/marvinblum.de/blog"
"github.com/Kugelschieber/marvinblum.de/tpl" "github.com/Kugelschieber/marvinblum.de/tpl"
"github.com/Kugelschieber/marvinblum.de/tracking" "github.com/Kugelschieber/marvinblum.de/tracking"
"github.com/NYTimes/gziphandler" "github.com/NYTimes/gziphandler"
"github.com/caddyserver/certmagic"
emvi "github.com/emvi/api-go" emvi "github.com/emvi/api-go"
"github.com/emvi/logbuch" "github.com/emvi/logbuch"
"github.com/emvi/pirsch" "github.com/emvi/pirsch"
@@ -15,6 +15,7 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"os" "os"
"os/signal"
"strings" "strings"
"time" "time"
) )
@@ -24,6 +25,7 @@ const (
staticDirPrefix = "/static/" staticDirPrefix = "/static/"
logTimeFormat = "2006-01-02_15:04:05" logTimeFormat = "2006-01-02_15:04:05"
envPrefix = "MB_" envPrefix = "MB_"
shutdownTimeout = time.Second * 30
) )
var ( var (
@@ -147,21 +149,28 @@ func configureCors(router *mux.Router) http.Handler {
func start(handler http.Handler) { func start(handler http.Handler) {
logbuch.Info("Starting server...") logbuch.Info("Starting server...")
var server http.Server
server.Handler = handler
server.Addr = os.Getenv("MB_HOST")
if strings.ToLower(os.Getenv("MB_TLS")) == "true" { go func() {
logbuch.Info("TLS enabled") sigint := make(chan os.Signal)
certmagic.DefaultACME.Agreed = true signal.Notify(sigint, os.Interrupt)
certmagic.DefaultACME.Email = os.Getenv("MB_TLS_EMAIL") <-sigint
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA logbuch.Info("Shutting down server...")
tracker.Stop()
ctx, _ := context.WithTimeout(context.Background(), shutdownTimeout)
if err := certmagic.HTTPS(strings.Split(os.Getenv("MB_DOMAIN"), ","), handler); err != nil { if err := server.Shutdown(ctx); err != nil {
logbuch.Fatal("Error starting server", logbuch.Fields{"err": err}) logbuch.Fatal("Error shutting down server gracefully", logbuch.Fields{"err": err})
}
} else {
if err := http.ListenAndServe(os.Getenv("MB_HOST"), handler); err != nil {
logbuch.Fatal("Error starting server", logbuch.Fields{"err": err})
} }
}()
if err := server.ListenAndServe(); err != http.ErrServerClosed {
logbuch.Fatal("Error starting server", logbuch.Fields{"err": err})
} }
logbuch.Info("Server shut down")
} }
func main() { func main() {

View File

@@ -7,7 +7,7 @@
This page uses <a href="https://concrete.style/" target="_blank">concrete</a> for styling. Check it out! This page uses <a href="https://concrete.style/" target="_blank">concrete</a> for styling. Check it out!
</p> </p>
<p> <p>
This page does not use Cookies. <a href="/legal">Legal</a> This page does not use cookies. <a href="/legal">Legal</a>
</p> </p>
</section> </section>
</body> </body>