Skip to content

Instantly share code, notes, and snippets.

@khannedy
Created April 26, 2022 03:04
Show Gist options
  • Save khannedy/08d52d1d9b7b41b34535df85509417b4 to your computer and use it in GitHub Desktop.
Save khannedy/08d52d1d9b7b41b34535df85509417b4 to your computer and use it in GitHub Desktop.
Golang Web with Health Check
package main
import (
"fmt"
"net/http"
)
var counter = 0
func main() {
http.HandleFunc("/", HelloServer)
http.HandleFunc("/health", HealthCheck)
http.ListenAndServe(":8080", nil)
}
func HealthCheck(w http.ResponseWriter, r *http.Request) {
counter = counter + 1
if counter > 5 {
w.WriteHeader(500)
fmt.Fprintf(w, "KO")
} else {
fmt.Fprintf(w, "OK")
}
}
func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment