Skip to content

Instantly share code, notes, and snippets.

@crazed
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazed/9904945 to your computer and use it in GitHub Desktop.
Save crazed/9904945 to your computer and use it in GitHub Desktop.
simple http proxy in go, with a chaos monkey twist
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