26 lines
540 B
Go
26 lines
540 B
Go
package serverctx
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const (
|
|
isHTTPS contextKey = "isHTTPS"
|
|
)
|
|
|
|
func Middleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
isHttps := r.URL.Scheme == "https" || r.Header.Get("X-Forwarded-Proto") == "https"
|
|
ctx := context.WithValue(r.Context(), isHTTPS, isHttps)
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
|
|
func IsHTTPS(ctx context.Context) bool {
|
|
isHttps, ok := ctx.Value(isHTTPS).(bool)
|
|
return ok && isHttps
|
|
}
|