token-validator: handle graceful shutdown

This commit is contained in:
nemunaire 2020-03-01 18:10:02 +01:00
parent 09552ab516
commit 72a4015288

View File

@ -1,12 +1,17 @@
package main package main
import ( import (
"context"
"flag" "flag"
"fmt"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os"
"os/signal"
"path" "path"
"strings" "strings"
"syscall"
) )
var sharedSecret string var sharedSecret string
@ -91,9 +96,25 @@ func main() {
log.Fatal("Cannot create database: ", err) log.Fatal("Cannot create database: ", err)
} }
// Prepare graceful shutdown
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
srv := &http.Server{
Addr: *bind,
Handler: StripPrefix(*baseURL, Router()),
}
// Serve content // Serve content
log.Println("Ready, listening on", *bind) go func() {
if err := http.ListenAndServe(*bind, StripPrefix(*baseURL, Router())); err != nil { log.Fatal(srv.ListenAndServe())
log.Fatal("Unable to listen and serve: ", err) }()
} log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
// Wait shutdown signal
<-interrupt
log.Print("The service is shutting down...")
srv.Shutdown(context.Background())
log.Println("done")
} }