mirror of
https://github.com/jlelse/GoBlog
synced 2024-06-14 14:37:09 +00:00
Fix HTTPS redirect
This commit is contained in:
parent
e9bbfc12d0
commit
806d588dca
11
http.go
11
http.go
|
@ -6,7 +6,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -66,7 +65,7 @@ func (a *goBlog) startServer() (err error) {
|
||||||
// Start HTTP server for redirects
|
// Start HTTP server for redirects
|
||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: ":http",
|
Addr: ":http",
|
||||||
Handler: http.HandlerFunc(redirectToHttps),
|
Handler: http.HandlerFunc(a.redirectToHttps),
|
||||||
ReadTimeout: 5 * time.Minute,
|
ReadTimeout: 5 * time.Minute,
|
||||||
WriteTimeout: 5 * time.Minute,
|
WriteTimeout: 5 * time.Minute,
|
||||||
}
|
}
|
||||||
|
@ -124,13 +123,9 @@ func shutdownServer(s *http.Server, name string) func() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func redirectToHttps(w http.ResponseWriter, r *http.Request) {
|
func (a *goBlog) redirectToHttps(w http.ResponseWriter, r *http.Request) {
|
||||||
requestHost, _, err := net.SplitHostPort(r.Host)
|
|
||||||
if err != nil {
|
|
||||||
requestHost = r.Host
|
|
||||||
}
|
|
||||||
w.Header().Set("Connection", "close")
|
w.Header().Set("Connection", "close")
|
||||||
http.Redirect(w, r, fmt.Sprintf("https://%s%s", requestHost, r.URL.RequestURI()), http.StatusMovedPermanently)
|
http.Redirect(w, r, fmt.Sprintf("https://%s%s", a.cfg.Server.publicHostname, r.URL.RequestURI()), http.StatusMovedPermanently)
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
Loading…
Reference in New Issue
Block a user