Skip to content

Instantly share code, notes, and snippets.

@nicolai86
Created October 11, 2014 15:16
Show Gist options
  • Save nicolai86/8f8ad32c9620b8620c63 to your computer and use it in GitHub Desktop.
Save nicolai86/8f8ad32c9620b8620c63 to your computer and use it in GitHub Desktop.
proxy + 2 http servers
package main
import (
"fmt"
"net/http"
)
func main() {
go func() {
server := http.Server{Addr: ":8081", Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte{})
})}
server.ListenAndServe()
}()
go func() {
server := http.Server{Addr: ":8082", Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte{})
})}
server.ListenAndServe()
}()
count := 0
server := http.Server{Addr: ":8080", Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
count = count + 1
client := http.Client{}
req.RequestURI = ""
req.URL.Scheme = "http"
if count%2 == 0 {
req.URL.Host = "127.0.0.1:8081"
} else {
req.URL.Host = "127.0.0.1:8082"
}
if _, err := client.Do(req); err != nil {
fmt.Printf("err: %v", err)
}
w.Write([]byte{})
})}
server.ListenAndServe()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment