File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,12 @@ type Config struct {
17
17
UserNameClaim string `envconfig:"default=email,optional"`
18
18
19
19
ShouldImpersonate bool `envconfig:"default=true,optional"`
20
+
21
+ Cors struct {
22
+ Enabled bool `envconfig:"default=false,optional"`
23
+ AllowedOrigins []string `envconfig:"default=*,optional"`
24
+ AllowedHeaders []string `envconfig:"default=*,optional"`
25
+ }
20
26
}
21
27
22
28
type HandlerConfig struct {
Original file line number Diff line number Diff line change @@ -187,6 +187,19 @@ func (s *Service) createHandler(schema *graphql.Schema) *graphqlHandler {
187
187
}
188
188
189
189
func (s * Service ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
190
+
191
+ if (* r ).Method == "OPTIONS" && s .appCfg .Cors .Enabled {
192
+ allowedOrigins := strings .Join (s .appCfg .Cors .AllowedOrigins , "," )
193
+ allowedHeaders := strings .Join (s .appCfg .Cors .AllowedHeaders , "," )
194
+ w .Header ().Set ("Access-Control-Allow-Origin" , allowedOrigins )
195
+ w .Header ().Set ("Access-Control-Allow-Headers" , allowedHeaders )
196
+ // setting cors allowed methods is not needed for this service,
197
+ // as all graphql methods are part of the cors safelisted methods
198
+ // https://fetch.spec.whatwg.org/#cors-safelisted-method
199
+ w .WriteHeader (http .StatusOK )
200
+ return
201
+ }
202
+
190
203
workspace , err := s .parsePath (r .URL .Path )
191
204
if err != nil {
192
205
s .log .Error ().Err (err ).Str ("path" , r .URL .Path ).Msg ("Error parsing path" )
You can’t perform that action at this time.
0 commit comments