mirror of
https://github.com/Kugelschieber/marvinblum.git
synced 2026-01-18 06:40:27 +00:00
Added graceful shutdown.
This commit is contained in:
33
main.go
33
main.go
@@ -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 shutting down server gracefully", logbuch.Fields{"err": err})
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
||||||
logbuch.Fatal("Error starting server", logbuch.Fields{"err": err})
|
logbuch.Fatal("Error starting server", logbuch.Fields{"err": err})
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if err := http.ListenAndServe(os.Getenv("MB_HOST"), handler); err != nil {
|
logbuch.Info("Server shut down")
|
||||||
logbuch.Fatal("Error starting server", logbuch.Fields{"err": err})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user