MCPcopy Create free account
hub / github.com/astercloud/aster / corsMiddleware

Function corsMiddleware

pkg/desktop/utils.go:36–59  ·  view source on GitHub ↗

corsMiddleware adds CORS headers

(next http.Handler)

Source from the content-addressed store, hash-verified

34
35// corsMiddleware adds CORS headers
36func corsMiddleware(next http.Handler) http.Handler {
37 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38 // Allow all origins for development
39 // In production, restrict to specific origins
40 origin := r.Header.Get("Origin")
41 if origin == "" {
42 origin = "*"
43 }
44
45 w.Header().Set("Access-Control-Allow-Origin", origin)
46 w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
47 w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
48 w.Header().Set("Access-Control-Allow-Credentials", "true")
49 w.Header().Set("Access-Control-Max-Age", "86400")
50
51 // Handle preflight
52 if r.Method == http.MethodOptions {
53 w.WriteHeader(http.StatusNoContent)
54 return
55 }
56
57 next.ServeHTTP(w, r)
58 })
59}

Callers 4

TestCorsMiddlewareFunction · 0.70
StartMethod · 0.70
StartMethod · 0.70
StartMethod · 0.70

Calls 3

GetMethod · 0.65
SetMethod · 0.65
ServeHTTPMethod · 0.45

Tested by 1

TestCorsMiddlewareFunction · 0.56