Skip to content

Instantly share code, notes, and snippets.

@joaofnds
Last active December 13, 2018 22:38
Show Gist options
  • Select an option

  • Save joaofnds/a2b5dce8ec40e0945632f5c92773a2e4 to your computer and use it in GitHub Desktop.

Select an option

Save joaofnds/a2b5dce8ec40e0945632f5c92773a2e4 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
port := os.Getenv("PORT")
if len(port) == 0 {
log.Fatalf("PORT environment not set")
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "Hello World")
})
addr := fmt.Sprintf(":%s", os.Getenv("PORT"))
s := http.Server{Addr: addr}
go (func() {
log.Fatal(s.ListenAndServe())
})()
log.Printf("Server is up and running at address '%s'", addr)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
log.Printf("Shutdown signal received, exiting...")
s.Shutdown(context.Background())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment