Skip to content

Instantly share code, notes, and snippets.

@piaoger
Created May 13, 2016 06:25
Show Gist options
  • Save piaoger/a54d79410687d67fe4940193f01ac2b1 to your computer and use it in GitHub Desktop.
Save piaoger/a54d79410687d67fe4940193f01ac2b1 to your computer and use it in GitHub Desktop.
reverseproxy.go
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
type handle struct {
host string
port string
}
func (this *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
remote, err := url.Parse("http://" + this.host + ":" + this.port)
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.ServeHTTP(w, r)
}
func startServer() {
h := &handle{host: "127.0.0.1", port: "80"}
err := http.ListenAndServe(":8888", h)
if err != nil {
log.Fatalln("ListenAndServe: ", err)
}
}
func main() {
startServer()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment