sklad: always try to shut down cleanly
This commit is contained in:
parent
6c6cec6298
commit
1bd7a9d735
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"html"
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
@ -10,11 +11,13 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"janouch.name/sklad/imgutil"
|
"janouch.name/sklad/imgutil"
|
||||||
|
@ -399,5 +402,22 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", handle)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue