-
Notifications
You must be signed in to change notification settings - Fork 30
/
cors.go
24 lines (20 loc) · 840 Bytes
/
cors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package interceptors
import (
"net/http"
"github.com/gorilla/handlers"
)
type CorsConfig struct {
AllowedOrigins []string `yaml:"allowed_origins" mapstructure:"allowed_origins" default:""`
AllowedMethods []string `yaml:"allowed_methods" mapstructure:"allowed_methods" default:"GET POST PUT DELETE PATCH"`
AllowedHeaders []string `yaml:"allowed_headers" mapstructure:"allowed_headers" default:"Authorization"`
ExposedHeaders []string `yaml:"exposed_headers" mapstructure:"exposed_headers" default:"Content-Type"`
}
func WithCors(h http.Handler, conf CorsConfig) http.Handler {
return handlers.CORS(
handlers.AllowedOrigins(conf.AllowedOrigins),
handlers.AllowedMethods(conf.AllowedMethods),
handlers.AllowedHeaders(conf.AllowedHeaders),
handlers.ExposedHeaders(conf.ExposedHeaders),
handlers.AllowCredentials(),
)(h)
}