fix(middleware): define middleware in a more streamlined way
All checks were successful
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 2m26s
All checks were successful
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 2m26s
This commit was merged in pull request #270.
This commit is contained in:
@@ -35,5 +35,9 @@ func GetHandler(d *sql.DB, serverSettings *types.ServerSettings) http.Handler {
|
|||||||
|
|
||||||
authHandler.handle(router)
|
authHandler.handle(router)
|
||||||
|
|
||||||
return middleware.Logging(middleware.ContentSecurityPolicy(middleware.EnableCors(serverSettings, router)))
|
return middleware.Wrapper(
|
||||||
|
router,
|
||||||
|
middleware.Log,
|
||||||
|
middleware.ContentSecurityPolicy,
|
||||||
|
middleware.Cors(serverSettings))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import "net/http"
|
|||||||
func ContentSecurityPolicy(next http.Handler) http.Handler {
|
func ContentSecurityPolicy(next http.Handler) http.Handler {
|
||||||
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// While this value can be overridden, it can't be moved to after the next.ServeHTTP call,
|
||||||
|
// because if the response writer get's closed, the headers can't be set anymore
|
||||||
w.Header().Set("Content-Security-Policy", "default-src 'self' https://umami.me-fit.eu")
|
w.Header().Set("Content-Security-Policy", "default-src 'self' https://umami.me-fit.eu")
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func EnableCors(serverSettings *types.ServerSettings, next http.Handler) http.Handler {
|
func Cors(serverSettings *types.ServerSettings) func(http.Handler) http.Handler {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", serverSettings.BaseUrl)
|
w.Header().Set("Access-Control-Allow-Origin", serverSettings.BaseUrl)
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE")
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE")
|
||||||
@@ -20,3 +20,4 @@ func EnableCors(serverSettings *types.ServerSettings, next http.Handler) http.Ha
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func (w *WrappedWriter) WriteHeader(code int) {
|
|||||||
w.StatusCode = code
|
w.StatusCode = code
|
||||||
}
|
}
|
||||||
|
|
||||||
func Logging(next http.Handler) http.Handler {
|
func Log(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
|
|||||||
13
middleware/wrapper.go
Normal file
13
middleware/wrapper.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func Wrapper(next http.Handler, handlers ...func(http.Handler) http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
lastHandler := next
|
||||||
|
for i := len(handlers) - 1; i >= 0; i-- {
|
||||||
|
lastHandler = handlers[i](lastHandler)
|
||||||
|
}
|
||||||
|
lastHandler.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user