Skip to content

Commit

Permalink
Support CORS in APIServer & handler ErrServerClosed (#1107)
Browse files Browse the repository at this point in the history
* Support CORS in APIServer & handler ErrServerClosed

* Fix mod inconsistency
  • Loading branch information
xxx7xxxx authored Oct 9, 2023
1 parent e09c7a9 commit ffa49b5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/fatih/color v1.15.0
github.com/fsnotify/fsnotify v1.6.0
github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/cors v1.2.1
github.com/go-logr/zapr v1.2.4
github.com/go-task/slim-sprig v2.20.0+incompatible
github.com/go-zookeeper/zk v1.0.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/dynamicmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"

"github.com/megaease/easegress/v2/pkg/logger"
)
Expand Down Expand Up @@ -84,6 +85,10 @@ func (m *dynamicMux) reloadAPIs() {
router.Use(m.basicAuth("easegress-basic-auth", m.server.opt.BasicAuth))
}

// For access from browser.
cors := cors.New(cors.Options{})
router.Use(cors.Handler)

for _, apiGroup := range apiGroups {
for _, api := range apiGroup.Entries {
pathV1 := APIPrefixV1 + api.Path
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func MustNewServer(opt *option.Options, cls cluster.Cluster, super *supervisor.S
logger.Infof("api server running in %s", opt.APIAddr)
err = s.server.ListenAndServe()
}
if err != nil {
if err != nil && err != http.ErrServerClosed {
logger.Errorf("start api server failed: %v", err)
}
}()
Expand Down

0 comments on commit ffa49b5

Please sign in to comment.