Skip to content

Commit 2e5ef1b

Browse files
committed
feat:支持配置cors allow domains
1 parent 2ce2f5a commit 2e5ef1b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

apiserver/httpserver/server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type HTTPServer struct {
6060
connLimitConfig *connlimit.Config
6161
tlsInfo *secure.TLSInfo
6262
option map[string]interface{}
63+
corsDomains []string
6364
openAPI map[string]apiserver.APIConfig
6465
start bool
6566
restart bool
@@ -142,6 +143,19 @@ func (h *HTTPServer) Initialize(_ context.Context, option map[string]interface{}
142143
}
143144
}
144145

146+
// cors domain
147+
if corsDomains, ok := option["corsDomains"].([]interface{}); ok && len(corsDomains) > 0 {
148+
log.Infof("[HTTPServer] option cors allow domains: %+v", corsDomains)
149+
h.corsDomains = make([]string, 0, len(corsDomains))
150+
for _, item := range corsDomains {
151+
domain, _ := item.(string)
152+
if domain != "" {
153+
h.corsDomains = append(h.corsDomains, domain)
154+
}
155+
}
156+
log.Infof("[HTTPServer] cors allow domains: %+v", h.corsDomains)
157+
}
158+
145159
metrics.SetMetricsPort(int32(h.listenPort))
146160
return nil
147161
}
@@ -331,6 +345,9 @@ func (h *HTTPServer) createRestfulContainer() (*restful.Container, error) {
331345
AllowedMethods: []string{"GET", "POST", "PUT"},
332346
CookiesAllowed: false,
333347
Container: wsContainer}
348+
if len(h.corsDomains) > 0 {
349+
cors.AllowedDomains = h.corsDomains
350+
}
334351
wsContainer.Filter(cors.Filter)
335352

336353
// Incr container filter to respond to OPTIONS

0 commit comments

Comments
 (0)