Skip to content
Snippets Groups Projects
Commit 04adf5fd authored by Frédéric Guillot's avatar Frédéric Guillot
Browse files

Add middleware to read X-Forwarded-Proto header

parent ddd3af4b
No related branches found
No related tags found
No related merge requests found
...@@ -39,6 +39,7 @@ func routes(cfg *config.Config, store *storage.Storage, feedHandler *feed.Handle ...@@ -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 { ...@@ -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)
......
// 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)
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment