Skip to content

Instantly share code, notes, and snippets.

@Xe
Created November 2, 2016 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xe/f6ef17ddfd0ae43ca30a8529f96f74b0 to your computer and use it in GitHub Desktop.
Save Xe/f6ef17ddfd0ae43ca30a8529f96f74b0 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
s := &http.Server{
Addr: ":9132",
}
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
f := w.(http.Flusher)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for {
select {
case <-req.Context().Done():
return
case <-ctx.Done():
return
default:
time.Sleep(1 * time.Second)
fmt.Fprintln(w, time.Now().String())
f.Flush()
}
}
})
http.HandleFunc("/anhero", func(w http.ResponseWriter, req *http.Request) {
go func() {
err := s.Shutdown(context.Background())
if err != nil {
panic(err)
}
}()
})
err := s.ListenAndServe()
if err != nil {
switch err {
case http.ErrServerClosed:
s.SetKeepAlivesEnabled(false)
time.Sleep(30 * time.Second)
log.Println("goodbye cruel world")
os.Exit(0)
default:
panic(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment