aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2019-04-19 12:26:27 +0200
committerPřemysl Janouch <p@janouch.name>2019-04-19 12:26:27 +0200
commit1bd7a9d73500007764fa73f16c039a6a111e3177 (patch)
treecded5ed5790f71dc20e3de0bd203668ef3d0258d /cmd
parent6c6cec62988d5d9ca48d3a23843393ca613a567b (diff)
downloadsklad-1bd7a9d73500007764fa73f16c039a6a111e3177.tar.gz
sklad-1bd7a9d73500007764fa73f16c039a6a111e3177.tar.xz
sklad-1bd7a9d73500007764fa73f16c039a6a111e3177.zip
sklad: always try to shut down cleanly
Diffstat (limited to 'cmd')
-rw-r--r--cmd/sklad/main.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/cmd/sklad/main.go b/cmd/sklad/main.go
index aebb5c1..475d214 100644
--- a/cmd/sklad/main.go
+++ b/cmd/sklad/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "context"
"errors"
"html"
"html/template"
@@ -10,11 +11,13 @@ import (
"net/http"
"net/url"
"os"
+ "os/signal"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
+ "syscall"
"time"
"janouch.name/sklad/imgutil"
@@ -399,5 +402,22 @@ func main() {
}
http.HandleFunc("/", handle)
- log.Fatalln(http.ListenAndServe(address, nil))
+ server := &http.Server{Addr: address}
+
+ sigs := make(chan os.Signal, 1)
+ errs := make(chan error, 1)
+ signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
+ go func() { errs <- server.ListenAndServe() }()
+
+ select {
+ case <-sigs:
+ case err := <-errs:
+ log.Println(err)
+ }
+
+ // Wait for all HTTP goroutines to finish so that not even the database
+ // log gets corrupted by an interrupted update.
+ if err := server.Shutdown(context.Background()); err != nil {
+ log.Fatalln(err)
+ }
}