1
0
Fork 0

Add middleware to read X-Forwarded-Proto header

This commit is contained in:
Frédéric Guillot 2018-04-27 22:25:00 -07:00
parent ddd3af4b85
commit 04adf5fdf5
3 changed files with 20 additions and 4 deletions

View file

@ -39,6 +39,7 @@ func routes(cfg *config.Config, store *storage.Storage, feedHandler *feed.Handle
router = router.PathPrefix(cfg.BasePath()).Subrouter()
}
router.Use(middleware.HeaderConfig)
router.Use(middleware.Logging)
router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {

View file

@ -34,10 +34,6 @@ func (h *Handler) Use(f ControllerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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)
request := NewRequest(r)
response := NewResponse(h.cfg, w, r, h.template)

View 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)
})
}