-
-
Save CoolHandDev/74a5a2e2128f02e38f68d06ca604d8b1 to your computer and use it in GitHub Desktop.
Revisions
-
peterhellberg revised this gist
Jun 13, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ func (s *Server) index(w http.ResponseWriter, r *http.Request) { } func main() { stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt) -
peterhellberg revised this gist
Jan 28, 2017 . No changes.There are no files selected for viewing
-
peterhellberg created this gist
Jan 28, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,75 @@ package main import ( "context" "log" "net/http" "os" "os/signal" "time" ) type Server struct { logger *log.Logger mux *http.ServeMux } func NewServer(options ...func(*Server)) *Server { s := &Server{ logger: log.New(os.Stdout, "", 0), mux: http.NewServeMux(), } for _, f := range options { f(s) } s.mux.HandleFunc("/", s.index) return s } func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.mux.ServeHTTP(w, r) } func (s *Server) index(w http.ResponseWriter, r *http.Request) { s.logger.Println("GET /") w.Write([]byte("Hello, World!")) } func main() { stop := make(chan os.Signal) signal.Notify(stop, os.Interrupt) logger := log.New(os.Stdout, "", 0) addr := ":" + os.Getenv("PORT") if addr == ":" { addr = ":2017" } s := NewServer(func(s *Server) { s.logger = logger }) h := &http.Server{Addr: addr, Handler: s} go func() { logger.Printf("Listening on http://0.0.0.0%s\n", addr) if err := h.ListenAndServe(); err != nil { logger.Fatal(err) } }() <-stop logger.Println("\nShutting down the server...") ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) h.Shutdown(ctx) logger.Println("Server gracefully stopped") }