35 lines
612 B
Go
35 lines
612 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
)
|
|
|
|
func fetchHandler(w http.ResponseWriter, r *http.Request) {
|
|
path := r.URL.Path
|
|
|
|
path = fmt.Sprintf("https://gravatar.com%s", path)
|
|
|
|
rpURL, _ := url.Parse(path)
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(rpURL)
|
|
director := proxy.Director
|
|
proxy.Director = func(r *http.Request) {
|
|
director(r)
|
|
r.Host = "gravatar.com"
|
|
}
|
|
proxy.ServeHTTP(w, r)
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/", fetchHandler)
|
|
|
|
err := http.ListenAndServe(fmt.Sprintf(":%d", 80), nil)
|
|
if err != nil {
|
|
fmt.Println("run server error: ", err)
|
|
return
|
|
}
|
|
}
|