11package main
22
33import (
4+ "context"
45 "fmt"
56 "io/ioutil"
7+ "log"
68 "net/http"
79 "os"
810)
@@ -14,33 +16,45 @@ func main() {
1416 }
1517 count := 0
1618
19+ m := http .NewServeMux ()
20+ s := http.Server {Addr : ":8080" , Handler : m }
21+
22+ log .Printf ("Server started\n " )
23+
1724 // Healthcheck endpoint
18- http .HandleFunc ("/healthz" , func (w http.ResponseWriter , r * http.Request ) {
25+ m .HandleFunc ("/healthz" , func (w http.ResponseWriter , r * http.Request ) {
1926 fmt .Fprintf (w , "OK" )
2027 })
2128
22- // Take one for the team
23- boom , _ := ioutil .ReadFile ("public/boom .html" )
24- http .HandleFunc ("/boom " , func (w http.ResponseWriter , r * http.Request ) {
29+ // Simulate failure
30+ boom , _ := ioutil .ReadFile ("public/shutdown .html" )
31+ m .HandleFunc ("/shutdown " , func (w http.ResponseWriter , r * http.Request ) {
2532 fmt .Fprintf (w , string (boom ))
26- fmt .Printf ("Goodbye\n " )
27- //os.Exit(0)
33+ log .Printf ("Received shutdown request\n " )
34+ go func () {
35+ if err := s .Shutdown (context .Background ()); err != nil {
36+ log .Fatal (err )
37+ }
38+ }()
2839 })
2940
3041 // Dashboard
3142 dashboard , _ := ioutil .ReadFile ("public/dashboard.html" )
32- http .HandleFunc ("/dashboard" , func (w http.ResponseWriter , r * http.Request ) {
43+ m .HandleFunc ("/dashboard" , func (w http.ResponseWriter , r * http.Request ) {
3344 fmt .Fprintf (w , string (dashboard ))
34- fmt .Printf ("GET %s\n " , r .URL .Path )
45+ log .Printf ("GET %s\n " , r .URL .Path )
3546 })
3647
3748 // Default
3849 index , _ := ioutil .ReadFile ("public/index.html" )
39- http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
50+ m .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
4051 count += 1
4152 fmt .Fprintf (w , string (index ), c , count )
42- fmt .Printf ("GET %s\n " , r .URL .Path )
53+ //log .Printf("GET %s\n", r.URL.Path)
4354 })
4455
45- http .ListenAndServe (":8080" , nil )
56+ if err := s .ListenAndServe (); err != nil && err != http .ErrServerClosed {
57+ log .Fatal (err )
58+ }
59+ log .Printf ("Exiting" )
4660}
0 commit comments