2017-12-02 16:03:36 +00:00
|
|
|
package middleware
|
|
|
|
|
2019-06-07 00:29:35 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
2017-12-02 16:03:36 +00:00
|
|
|
|
2019-06-07 00:29:35 +00:00
|
|
|
"github.com/aerogo/aero"
|
|
|
|
)
|
2017-12-02 16:03:36 +00:00
|
|
|
|
2019-06-07 00:29:35 +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)
|
|
|
|
}
|
|
|
|
}
|