Created
February 14, 2020 11:34
-
-
Save quii/5de3c95dd71925fe5daecd17f16ddd05 to your computer and use it in GitHub Desktop.
concurrent stitcher
This file contains 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
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