Created
November 11, 2019 09:35
-
-
Save nabam/a3ac0c73f1d1d3a322052c2ea0f9632f to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"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