Issue
I have a server written in Go. I want to write for it a reverse proxy server. The server is compiled into one binary file. When I try to access it through a proxy server, it returns only the HTML page without bindings to CSS and JS scripts. How can I organize the transfer of static files?
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
func main() {
mux := http.NewServeMux()
u1, _ := url.Parse("http://localhost:8080/")
proxy := httputil.NewSingleHostReverseProxy(u1)
mux.Handle("/app1", proxy)
serv := &http.Server{
Addr: ":9090",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: mux,
}
err := serv.ListenAndServe()
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Solution
Writing a reverse proxy is a complex task. If you plan to expose it on the public internet, I suggest to use an existing one, e.g. Caddyserver, which is written in Go and still free if you compile it yourself instead of using the pre-compiled binaries. The source code of Caddyserver can also give you ideas how to implement it on your own, if you still want to pursue that idea.
To your more specific question about serving files: This can be done with the http.ServeFile method or easier with httprouter's ServeFiles or FileServer methods. See https://github.com/julienschmidt/httprouter#static-files.
Answered By - Chris Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.