cors.go 628 B

1234567891011121314151617181920212223
  1. package cors
  2. import (
  3. log "github.com/sirupsen/logrus"
  4. "net/http"
  5. )
  6. // AllowCors takes a HTTP handler and adds Access-Control-Allow-Origin headers to
  7. // responses.
  8. //
  9. // Note: HTTP OPTIONS requests (which need to be preflighted" for CORS) are not
  10. // handled because this app does not use HTTP PUT/PATCH/etc.
  11. func AllowCors(h http.Handler) http.Handler {
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. if origin := r.Header.Get("Origin"); origin != "" {
  14. log.Debugf("Adding CORS header origin: %q", origin)
  15. w.Header().Set("Access-Control-Allow-Origin", origin)
  16. }
  17. h.ServeHTTP(w, r)
  18. })
  19. }