diff options
| author | Adnan Maolood <[email protected]> | 2021-02-21 00:29:38 -0500 |
|---|---|---|
| committer | Adnan Maolood <[email protected]> | 2021-02-21 00:29:41 -0500 |
| commit | 6c701ad9fe081204a4ac0373a41aa93bd8568467 (patch) | |
| tree | a7a582b455345ebdb012a4fcce49c2403bd563f5 /examples/stream.go | |
| parent | examples: Use context (diff) | |
| download | go-gemini-6c701ad9fe081204a4ac0373a41aa93bd8568467.tar.xz go-gemini-6c701ad9fe081204a4ac0373a41aa93bd8568467.zip | |
examples/stream: Showcase Server.Shutdown method
Diffstat (limited to 'examples/stream.go')
| -rw-r--r-- | examples/stream.go | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/examples/stream.go b/examples/stream.go index 607ae1c..f64f87b 100644 --- a/examples/stream.go +++ b/examples/stream.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "log" + "sync" "time" "git.sr.ht/~adnano/go-gemini" @@ -31,8 +32,23 @@ func main() { GetCertificate: certificates.GetCertificate, } - if err := server.ListenAndServe(); err != nil { - log.Fatal(err) + var shutdownOnce sync.Once + 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) } } |