Skip to content

Instantly share code, notes, and snippets.

@quii
Created February 14, 2020 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quii/5de3c95dd71925fe5daecd17f16ddd05 to your computer and use it in GitHub Desktop.
Save quii/5de3c95dd71925fe5daecd17f16ddd05 to your computer and use it in GitHub Desktop.
concurrent stitcher
func Stitcher(w http.ResponseWriter, r *http.Request) {
urls := r.URL.Query()["url"]
if len(urls) == 0 {
http.Error(w, "please provide a url querystring value", http.StatusBadRequest)
return
}
bodies := make(chan io.ReadCloser, len(urls))
for _, url := range urls {
go func(bodies chan io.ReadCloser, url string) {
res, _ := http.Get(url)
bodies <- res.Body
}(bodies, url)
}
for i:=0; i < len(urls); i++ {
body := <-bodies
defer body.Close()
io.Copy(w, body)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment