File size: 831 Bytes
d6f631f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package server

import (
	"net/http"
	"strings"

	"github.com/go-chi/cors"
)

type corsOptions struct {
	cors.Options
	AllowedPaths []string
}

func corsHandler(options corsOptions) func(next http.Handler) http.Handler {
	ch := cors.Handler(options.Options)

	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			// If AllowedPaths is empty, apply CORS to all paths
			if len(options.AllowedPaths) == 0 {
				ch(next).ServeHTTP(w, r)
				return
			}

			// Check if the request path starts with any of the allowed prefixes
			for _, path := range options.AllowedPaths {
				if strings.HasPrefix(r.URL.Path, path) {
					ch(next).ServeHTTP(w, r)
					return
				}
			}

			// If none of the prefixes match, call the next handler
			next.ServeHTTP(w, r)
		})
	}
}