Created
July 3, 2019 12:55
-
-
Save peteretelej/02fd1134a61b3a11cef425f24b65631f to your computer and use it in GitHub Desktop.
A simple reverse-proxy in Go that enables CORS
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" | |
) | |
func main() { | |
http.HandleFunc("/", handler) | |
log.Print("listening :8080") | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} | |
var u, _ = url.Parse("https://jobs.github.com/") | |
func handler(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Access-Control-Allow-Origin", "*") | |
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") | |
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") | |
proxy := httputil.NewSingleHostReverseProxy(u) | |
r.URL.Host = u.Host | |
r.URL.Scheme = u.Scheme | |
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host")) | |
r.Host = u.Host | |
w.Header().Set("Access-Control-Allow-Origin", "*") | |
proxy.ServeHTTP(w, r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment