1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-05-19 00:46:04 +02:00

examples/server: Shutdown on interrupt signal

This commit is contained in:
Adnan Maolood 2021-02-22 21:14:41 -05:00
parent 35f7958083
commit 9aebcd362e

View File

@ -8,6 +8,7 @@ import (
"context"
"log"
"os"
"os/signal"
"time"
"git.sr.ht/~adnano/go-gemini"
@ -31,7 +32,26 @@ func main() {
GetCertificate: certificates.GetCertificate,
}
if err := server.ListenAndServe(context.Background()); err != nil {
// Listen for interrupt signal
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
errch := make(chan error)
go func() {
ctx := context.Background()
errch <- server.ListenAndServe(ctx)
}()
select {
case err := <-errch:
log.Fatal(err)
case <-c:
// Shutdown the server
log.Println("Shutting down...")
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err := server.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
}
}