24 lines
520 B
Go
Raw Normal View History

2017-12-02 16:03:36 +00:00
package middleware
import (
"net/http"
2017-12-02 16:03:36 +00:00
"github.com/aerogo/aero"
)
2017-12-02 16:03:36 +00:00
// HTTPSRedirect middleware redirects to HTTPS if needed.
func HTTPSRedirect(next aero.Handler) aero.Handler {
return func(ctx aero.Context) error {
request := ctx.Request()
userAgent := request.Header("User-Agent")
isBrowser := userAgent != ""
if isBrowser && request.Scheme() != "https" {
return ctx.Redirect(http.StatusPermanentRedirect, "https://"+request.Host()+request.Path())
}
// Handle the request
return next(ctx)
}
}