update CORS middleware and make it configurable
This commit is contained in:
@@ -11,15 +11,19 @@ type Option func(o *Options)
|
||||
|
||||
// Options defines the available options for this package.
|
||||
type Options struct {
|
||||
Logger log.Logger
|
||||
Name string
|
||||
Version string
|
||||
Address string
|
||||
Token string
|
||||
Pprof bool
|
||||
Zpages bool
|
||||
Health func(http.ResponseWriter, *http.Request)
|
||||
Ready func(http.ResponseWriter, *http.Request)
|
||||
Logger log.Logger
|
||||
Name string
|
||||
Version string
|
||||
Address string
|
||||
Token string
|
||||
Pprof bool
|
||||
Zpages bool
|
||||
Health func(http.ResponseWriter, *http.Request)
|
||||
Ready func(http.ResponseWriter, *http.Request)
|
||||
CorsAllowedOrigins []string
|
||||
CorsAllowedMethods []string
|
||||
CorsAllowedHeaders []string
|
||||
CorsAllowCredentials bool
|
||||
}
|
||||
|
||||
// newOptions initializes the available default options.
|
||||
@@ -95,3 +99,31 @@ func Ready(r func(http.ResponseWriter, *http.Request)) Option {
|
||||
o.Ready = r
|
||||
}
|
||||
}
|
||||
|
||||
// CorsAllowedOrigins provides a function to set the CorsAllowedOrigin option.
|
||||
func CorsAllowedOrigins(origins []string) Option {
|
||||
return func(o *Options) {
|
||||
o.CorsAllowedOrigins = origins
|
||||
}
|
||||
}
|
||||
|
||||
// CorsAllowedMethods provides a function to set the CorsAllowedMethods option.
|
||||
func CorsAllowedMethods(methods []string) Option {
|
||||
return func(o *Options) {
|
||||
o.CorsAllowedMethods = methods
|
||||
}
|
||||
}
|
||||
|
||||
// CorsAllowedHeaders provides a function to set the CorsAllowedHeaders option.
|
||||
func CorsAllowedHeaders(headers []string) Option {
|
||||
return func(o *Options) {
|
||||
o.CorsAllowedHeaders = headers
|
||||
}
|
||||
}
|
||||
|
||||
// CorsAllowCredentials provides a function to set the CorsAllowAllowCredential option.
|
||||
func CorsAllowCredentials(allow bool) Option {
|
||||
return func(o *Options) {
|
||||
o.CorsAllowCredentials = allow
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/justinas/alice"
|
||||
"github.com/owncloud/ocis/ocis-pkg/cors"
|
||||
"github.com/owncloud/ocis/ocis-pkg/middleware"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"go.opencensus.io/zpages"
|
||||
@@ -51,7 +52,12 @@ func NewService(opts ...Option) *http.Server {
|
||||
chimiddleware.RealIP,
|
||||
chimiddleware.RequestID,
|
||||
middleware.NoCache,
|
||||
middleware.Cors,
|
||||
middleware.Cors(
|
||||
cors.AllowedOrigins(dopts.CorsAllowedOrigins),
|
||||
cors.AllowedMethods(dopts.CorsAllowedMethods),
|
||||
cors.AllowedHeaders(dopts.CorsAllowedHeaders),
|
||||
cors.AllowCredentials(dopts.CorsAllowCredentials),
|
||||
),
|
||||
middleware.Secure,
|
||||
middleware.Version(
|
||||
dopts.Name,
|
||||
|
||||
Reference in New Issue
Block a user