Answer the question
In order to leave comments, you need to log in
Is it possible to implement an analogue of proxy_pass nginx in golang?
Nginx is very cool at proxying requests and downloading files, but the problem is to authorize these requests and change urls dynamically.
But it’s enough to write
location /start {
proxy_pass http://kinosotik.com/download/malenqkie-chudoviwa?original;
}
func main() {
http.HandleFunc("/", handlerProxy)
if err := http.ListenAndServe(":4000", nil); err != nil {
panic(err)
}
}
func handlerProxy(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Host)
url, _ := url.Parse(fmt.Sprintf("http://kinosotik.com/download/malenqkie-chudoviwa?original"))
r.Host = url.Host
r.URL.Scheme = "http"
proxy := httputil.NewSingleHostReverseProxy(url)
fmt.Println(r.URL.Host)
proxy.ServeHTTP(w, r)
}
Answer the question
In order to leave comments, you need to log in
to have no buffer, you need this piece
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, "error reading response body", http.StatusInternalServerError)
return
}
// write status code and body from proxy request into the answer
w.WriteHeader(resp.StatusCode)
w.Write(body)
w.WriteHeader(resp.StatusCode)
_, err := io.Copy(w, body)
if err != nil {
http.Error(w, "error reading response body", http.StatusInternalServerError)
return
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question