Last active
December 13, 2018 22:38
-
-
Save joaofnds/a2b5dce8ec40e0945632f5c92773a2e4 to your computer and use it in GitHub Desktop.
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 characters
| 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