3

I'm using Revel for a small app and added SSL. When I update the config to point to http.port = 443, requests to port 80 are rejected instead of being forwarded. Is there a way to fix this on the Revel Framework? Thank you.

# The port on which to listen.
http.port = 443

# Whether to use SSL or not.
http.ssl = true

# Path to an X509 certificate file, if using SSL.
http.sslcert = /root/go/src/saml/AlphaCerts/cert.crt

# Path to an X509 certificate key, if using SSL.
http.sslkey = /root/go/src/saml/star_home_com.key
vinniyo
  • 787
  • 2
  • 8
  • 21

1 Answers1

5

You could add a simple redirect handler yourself.

Try putting this in your app/init.go file:

httpRedirectServer := &http.Server{Addr: ":80", Handler: http.HandlerFunc(
    func(w http.ResponseWriter, r *http.Request) {
        http.Redirect(w, r, fmt.Sprintf("https://%s%s", r.Host, r.RequestURI), 
                      http.StatusMovedPermanently)
    })}
go httpRedirectServer.ListenAndServe()

Note that in Revel's dev mode, you'll first have to access your app on port 443 to have Revel start up properly before your port 80 redirect code will be run.

Colin Nicholson
  • 1,331
  • 12
  • 18
  • This worked without any changes to your code. A note for others to import the net/http and fmt after placing the above code in the func init(). Thank you! – vinniyo Nov 06 '15 at 21:03