forked from joeholley/supergloo
/
main.go
42 lines (36 loc) · 914 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
)
func main() {
port := flag.Uint64("p", 8080, "port")
failEvenRequests := flag.Bool("fail-half", false, "fail every other request. used to test retries")
flag.Parse()
log.Fatal(run(*port, *failEvenRequests))
}
func logReq(r *http.Request) {
log.Printf("request count: %v", requestCount)
b, err := httputil.DumpRequest(r, true)
if err != nil {
log.Printf("error dumping req: %v", err)
} else {
log.Printf("request dump:\n%s\n", b)
}
}
var requestCount int
func run(port uint64, failEvenRequests bool) error {
m := http.NewServeMux()
m.HandleFunc("/retry-this-route", func(w http.ResponseWriter, r *http.Request) {
logReq(r)
requestCount++
if failEvenRequests && requestCount%2 == 0 {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
return http.ListenAndServe(fmt.Sprintf(":%v", port), m)
}