Last active
August 29, 2015 13:57
-
-
Save crazed/9904945 to your computer and use it in GitHub Desktop.
simple http proxy in go, with a chaos monkey twist
This file contains hidden or 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" | |
"net/http" | |
"net/http/httputil" | |
"strconv" | |
"time" | |
) | |
const VERSION = "0.0.1" | |
func HttpRouteHandler(w http.ResponseWriter, r *http.Request) { | |
r.URL.Scheme = "http" | |
r.URL.Host = r.Host | |
for key, val := range r.Header { | |
switch key { | |
case "X-Chaos-Latency": | |
sleepInt, err := strconv.Atoi(val[0]) | |
if err != nil { | |
fmt.Println("Failed to convert X-Chaos-Latency to integer") | |
return | |
} | |
sleep := time.Duration(sleepInt) * time.Millisecond | |
fmt.Println("Adding", sleep, "to request.") | |
time.Sleep(sleep) | |
delete(r.Header, key) | |
} | |
} | |
proxy := httputil.NewSingleHostReverseProxy(r.URL) | |
proxy.ServeHTTP(w, r) | |
} | |
func main() { | |
http.HandleFunc("/", HttpRouteHandler) | |
http.ListenAndServe(":80", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment