I am trying to redirect the API call to HTTPS if an api call is made to HTTP, But I get an nil in r.URL.Scheme and r.TLS for both HTTP and HTTPS calls
func main() {
r := mux.NewRouter()
r.HandleFunc("/info", infoHandler).Methods("GET")
portString := fmt.Sprintf(":%s", getPorts())
if cfenv.IsRunningOnCF() == true {
r.Use(redirectTLS)
}
if err := http.ListenAndServe(portString, r); err != nil {
panic(err)
}
}
func redirectTLS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//if r.URL.Scheme != "https" {
if r.TLS == nil {
log.Info("Redirecting to HTTPS")
//targetUrl := url.URL{Scheme: "https", Host: r.Host, Path: r.URL.Path, RawQuery: r.URL.RawQuery}
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusMovedPermanently)
//http.Redirect(w, r, targetUrl, http.StatusMovedPermanently)
return
}
log.Info("Not Redirecting to HTTPS")
next.ServeHTTP(w, r)
return
})
}