Skip to content

Instantly share code, notes, and snippets.

@nabam
Created November 11, 2019 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabam/a3ac0c73f1d1d3a322052c2ea0f9632f to your computer and use it in GitHub Desktop.
Save nabam/a3ac0c73f1d1d3a322052c2ea0f9632f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"io/ioutil"
"net/http"
)
var state = make(map[string]int)
func handler(w http.ResponseWriter, r *http.Request) {
bodyBuffer, _ := ioutil.ReadAll(r.Body)
fmt.Printf("%s\n", bodyBuffer)
state[r.URL.Path] = state[r.URL.Path] + 1
if state[r.URL.Path] <= 4 {
http.Error(w, "Oops", http.StatusServiceUnavailable)
fmt.Printf("%s sent 503 on %d try!\n", r.URL.Path, state[r.URL.Path])
return
}
fmt.Printf("%s sent 200 on %d try!\n", r.URL.Path, state[r.URL.Path])
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment