diff --git a/pkg/api/middleware.go b/pkg/api/middleware.go index 4139aaa..ef750f7 100644 --- a/pkg/api/middleware.go +++ b/pkg/api/middleware.go @@ -76,3 +76,11 @@ func serveResult(result interface{}, err error, c *gin.Context) { c.JSON(200, result) } + +func corsMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + c.Header("Access-Control-Allow-Methods", "GET, POST, OPTIONS") + c.Header("Access-Control-Allow-Origin", "*") + c.Header("Access-Control-Expose-Headers", "*") + } +} diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 182a16b..b8fea12 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -10,6 +10,10 @@ func SetupMiddlewares(group *gin.RouterGroup) { group.Use(requestInspectMiddleware()) } + if command.Opts.CORS { + group.Use(corsMiddleware()) + } + group.Use(dbCheckMiddleware()) } diff --git a/pkg/command/options.go b/pkg/command/options.go index 42dc924..deeaa25 100644 --- a/pkg/command/options.go +++ b/pkg/command/options.go @@ -36,6 +36,7 @@ type Options struct { ConnectHeaders string `long:"connect-headers" description:"List of headers to pass to the connect backend"` DisableConnectionIdleTimeout bool `long:"no-idle-timeout" description:"Disable connection idle timeout" default:"false"` ConnectionIdleTimeout float64 `long:"idle-timeout" description:"Set connection idle timeout in minutes" default:"180"` + CORS bool `long:"cors" description:"Enable Cross-Origin Resource Sharing (CORS)" default:"false"` } var Opts Options