diff --git a/go.mod b/go.mod index 1ab551b832..4b37cf3c03 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 459da85fd2..52e1468dfc 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/api/dynamicmux.go b/pkg/api/dynamicmux.go index 7b7f05d62f..b48a14e660 100644 --- a/pkg/api/dynamicmux.go +++ b/pkg/api/dynamicmux.go @@ -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" ) @@ -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 diff --git a/pkg/api/server.go b/pkg/api/server.go index df07aaa6a9..3228e968f6 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -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) } }()