Created
May 13, 2016 06:25
-
-
Save piaoger/a54d79410687d67fe4940193f01ac2b1 to your computer and use it in GitHub Desktop.
reverseproxy.go
This file contains hidden or 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
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