token-validator: handle graceful shutdown
This commit is contained in:
parent
09552ab516
commit
72a4015288
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve content
|
// Prepare graceful shutdown
|
||||||
log.Println("Ready, listening on", *bind)
|
interrupt := make(chan os.Signal, 1)
|
||||||
if err := http.ListenAndServe(*bind, StripPrefix(*baseURL, Router())); err != nil {
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
||||||
log.Fatal("Unable to listen and serve: ", err)
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: *bind,
|
||||||
|
Handler: StripPrefix(*baseURL, Router()),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serve content
|
||||||
|
go func() {
|
||||||
|
log.Fatal(srv.ListenAndServe())
|
||||||
|
}()
|
||||||
|
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")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user