2
0
mirror of https://github.com/offen/website.git synced 2024-11-26 18:50:27 +01:00
website/shared/http/middleware.go

30 lines
869 B
Go
Raw Normal View History

2019-06-06 13:06:31 +02:00
package http
import "net/http"
func CorsMiddleware(next http.Handler, origin string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Methods", "POST,GET")
w.Header().Set("Access-Control-Allow-Origin", origin)
next.ServeHTTP(w, r)
})
}
func ContentTypeMiddleware(next http.Handler, contentType string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", contentType)
next.ServeHTTP(w, r)
})
}
2019-06-24 22:26:50 +02:00
func OptoutMiddleware(next http.Handler) http.Handler {
2019-06-06 13:06:31 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2019-06-24 22:26:50 +02:00
if _, err := r.Cookie("optout"); err == nil {
2019-06-06 13:06:31 +02:00
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
}