examples/stream: Showcase Server.Shutdown method

This commit is contained in:
Adnan Maolood 2021-02-21 00:29:38 -05:00
parent 7084a226f4
commit 6c701ad9fe

View File

@ -8,6 +8,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"sync"
"time" "time"
"git.sr.ht/~adnano/go-gemini" "git.sr.ht/~adnano/go-gemini"
@ -31,8 +32,23 @@ func main() {
GetCertificate: certificates.GetCertificate, GetCertificate: certificates.GetCertificate,
} }
if err := server.ListenAndServe(); err != nil { var shutdownOnce sync.Once
log.Fatal(err) var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
mux.HandleFunc("/shutdown", func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
fmt.Fprintln(w, "Shutting down...")
if flusher, ok := w.(gemini.Flusher); ok {
flusher.Flush()
}
go shutdownOnce.Do(func() {
server.Shutdown(context.Background())
wg.Done()
})
})
if err := server.ListenAndServe(context.Background()); err != nil {
log.Println(err)
} }
} }