Add middleware to read X-Forwarded-Proto header
This commit is contained in:
parent
ddd3af4b85
commit
04adf5fdf5
3 changed files with 20 additions and 4 deletions
|
@ -39,6 +39,7 @@ func routes(cfg *config.Config, store *storage.Storage, feedHandler *feed.Handle
|
||||||
router = router.PathPrefix(cfg.BasePath()).Subrouter()
|
router = router.PathPrefix(cfg.BasePath()).Subrouter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
router.Use(middleware.HeaderConfig)
|
||||||
router.Use(middleware.Logging)
|
router.Use(middleware.Logging)
|
||||||
|
|
||||||
router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
|
router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -34,10 +34,6 @@ func (h *Handler) Use(f ControllerFunc) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
defer timer.ExecutionTime(time.Now(), r.URL.Path)
|
defer timer.ExecutionTime(time.Now(), r.URL.Path)
|
||||||
|
|
||||||
if r.Header.Get("X-Forwarded-Proto") == "https" {
|
|
||||||
h.cfg.IsHTTPS = true
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := NewContext(r, h.store, h.router, h.translator)
|
ctx := NewContext(r, h.store, h.router, h.translator)
|
||||||
request := NewRequest(r)
|
request := NewRequest(r)
|
||||||
response := NewResponse(h.cfg, w, r, h.template)
|
response := NewResponse(h.cfg, w, r, h.template)
|
||||||
|
|
19
middleware/header_config.go
Normal file
19
middleware/header_config.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
||||||
|
// Use of this source code is governed by the Apache 2.0
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HeaderConfig changes config values according to HTTP headers.
|
||||||
|
func (m *Middleware) HeaderConfig(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Header.Get("X-Forwarded-Proto") == "https" {
|
||||||
|
m.cfg.IsHTTPS = true
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue