Files
OliveTin/service/internal/cors/cors.go
James Read ff31abe66c
Some checks failed
Build Snapshot / build-snapshot (push) Waiting to run
DevSkim / DevSkim (push) Waiting to run
Buf CI / buf (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Codestyle checks / codestyle (push) Has been cancelled
refactor: Project directories (#541)
2025-03-22 01:06:59 +00:00

24 lines
628 B
Go

package cors
import (
log "github.com/sirupsen/logrus"
"net/http"
)
// AllowCors takes a HTTP handler and adds Access-Control-Allow-Origin headers to
// responses.
//
// Note: HTTP OPTIONS requests (which need to be preflighted" for CORS) are not
// handled because this app does not use HTTP PUT/PATCH/etc.
func AllowCors(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
log.Debugf("Adding CORS header origin: %q", origin)
w.Header().Set("Access-Control-Allow-Origin", origin)
}
h.ServeHTTP(w, r)
})
}