Created
October 16, 2020 14:46
-
-
Save yakutozcan/e779f5940f3155fe29ee0d2ffa18a3c9 to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
"os" | |
) | |
func getEnv(key, fallback string) string { | |
if value, ok := os.LookupEnv(key); ok { | |
return value | |
} | |
return fallback | |
} | |
func getListenAddress() string { | |
port := getEnv("PORT", "1338") | |
return ":" + port | |
} | |
func getFCMToken() string { | |
token := getEnv("FCM_TOKEN", | |
"token_token_token_token_token_token_token_token") | |
return token | |
} | |
func serveReverseProxy(target string, res http.ResponseWriter, req *http.Request) { | |
fcmUrl, _ := url.Parse(target) | |
proxy := httputil.NewSingleHostReverseProxy(fcmUrl) | |
req.URL.Host = fcmUrl.Host | |
req.URL.Scheme = fcmUrl.Scheme | |
req.Header.Set("X-Forwarded-Host", req.Header.Get("Host")) | |
req.Header.Set("Authorization", "key="+getFCMToken()) | |
req.Host = fcmUrl.Host | |
proxy.ServeHTTP(res, req) | |
} | |
func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) { | |
fmt.Println(req.Header.Get("Authorization"))//todo magic | |
serveReverseProxy("https://fcm.googleapis.com", res, req) | |
} | |
func main() { | |
http.HandleFunc("/", handleRequestAndRedirect) | |
if err := http.ListenAndServe(getListenAddress(), nil); err != nil { | |
fmt.Println("HTTP Server", err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment