Created
October 11, 2014 15:16
-
-
Save nicolai86/8f8ad32c9620b8620c63 to your computer and use it in GitHub Desktop.
proxy + 2 http servers
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" | |
) | |
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