MCPcopy Create free account
hub / github.com/ericls/imgdd / makeImageCORSMiddleware

Function makeImageCORSMiddleware

httpserver/image_cors.go:5–22  ·  view source on GitHub ↗
(allowedOrigins []string)

Source from the content-addressed store, hash-verified

3import "net/http"
4
5func makeImageCORSMiddleware(allowedOrigins []string) func(http.Handler) http.Handler {
6 allowed := make(map[string]struct{}, len(allowedOrigins))
7 for _, o := range allowedOrigins {
8 allowed[o] = struct{}{}
9 }
10 return func(next http.Handler) http.Handler {
11 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12 origin := r.Header.Get("Origin")
13 if origin != "" {
14 if _, ok := allowed[origin]; ok {
15 w.Header().Set("Access-Control-Allow-Origin", origin)
16 w.Header().Add("Vary", "Origin")
17 }
18 }
19 next.ServeHTTP(w, r)
20 })
21 }
22}

Callers 1

MakeServerFunction · 0.85

Calls 2

GetMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected