-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirect.go
More file actions
29 lines (26 loc) · 789 Bytes
/
redirect.go
File metadata and controls
29 lines (26 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package easyhttps
import (
"context"
"fmt"
"net/http"
"strings"
"golang.org/x/crypto/acme/autocert"
)
func (s *HTTPSServer) makeHTTPToHTTPSRedirectMux(m *autocert.Manager) http.HandlerFunc {
hasHTTPS := false
handleRedirect := func(w http.ResponseWriter, r *http.Request) {
newURI := "https://" + r.Host + r.URL.String()
http.Redirect(w, r, newURI, http.StatusFound)
}
callbackHandler := m.HTTPHandler(nil)
return func(w http.ResponseWriter, r *http.Request) {
if _, err := m.Cache.Get(context.Background(), s.AllowedHost); strings.HasPrefix(r.URL.Path, "/.well-known/acme-challenge/") || err == nil {
callbackHandler.ServeHTTP(w, r)
hasHTTPS = true
} else if hasHTTPS {
handleRedirect(w, r)
} else {
fmt.Fprint(w, "Waiting on TLS Certificate")
}
}
}