Created
February 6, 2014 18:17
-
-
Save taylorhughes/8849605 to your computer and use it in GitHub Desktop.
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 ( | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
"os" | |
) | |
type SingleRequestTransport struct { | |
bottleneck chan bool | |
} | |
func (s *SingleRequestTransport) RoundTrip(req *http.Request) (*http.Response, error) { | |
s.bottleneck <- true | |
response, err := http.DefaultTransport.RoundTrip(req) | |
<- s.bottleneck | |
return response, err; | |
} | |
func main() { | |
if len(os.Args) != 3 { | |
log.Fatal("Usage: go run ./devproxy.go [listen host and port] [to host and port]") | |
} | |
hostAndPort := os.Args[1] | |
proxyHost := os.Args[2] | |
proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: proxyHost, Path: "/"}) | |
proxy.Transport = &SingleRequestTransport{bottleneck: make(chan bool, 1)} | |
server := &http.Server{ | |
Addr: hostAndPort, | |
Handler: proxy, | |
} | |
log.Print("Dev proxy listening on: ", hostAndPort, " redirecting to: ", proxyHost) | |
err := server.ListenAndServe() | |
if (err != nil) { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple reverse proxy that makes requests to the backend happen sequentially. This is for development use when your development server sucks at handling concurrent requests. (I'm looking at you, Django.)