Skip to content

Instantly share code, notes, and snippets.

@Xe Xe/main.go
Created Nov 2, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.