Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active August 5, 2022 05:26
Show Gist options
  • Save percybolmer/259cbf8a37efd39f7eb4fe273a200cc6 to your computer and use it in GitHub Desktop.
Save percybolmer/259cbf8a37efd39f7eb4fe273a200cc6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func main() {
// Grab time of startup
started := time.Now()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Calculate runtime duration
duration := time.Now().Sub(started)
if duration.Seconds() > 10 {
log.Println("Timeout triggered")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`hello gopher`))
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`hello gopher`))
}
})
http.HandleFunc("/aligator", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi Mr Aligator")
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment