From 3455eb39b3d9a5dfbf54a809b257d5f421805078 Mon Sep 17 00:00:00 2001 From: Namit Chandwani Date: Wed, 10 Mar 2021 05:13:29 +0530 Subject: [PATCH] fix: add CORS setup --- go.mod | 1 + go.sum | 2 ++ internal/cmd/serve/cmd.go | 8 +++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 05bf048..7d58430 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/golang/protobuf v1.3.1 github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pkg/errors v0.9.1 + github.com/rs/cors v1.7.0 // indirect github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd github.com/spf13/cobra v1.0.0 diff --git a/go.sum b/go.sum index 48ecc8e..126b716 100644 --- a/go.sum +++ b/go.sum @@ -121,6 +121,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= +github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/segmentio/ksuid v1.0.2 h1:9yBfKyw4ECGTdALaF09Snw3sLJmYIX6AbPJrAy6MrDc= github.com/segmentio/ksuid v1.0.2/go.mod h1:BXuJDr2byAiHuQaQtSKoXh1J0YmUDurywOXgB2w+OSU= diff --git a/internal/cmd/serve/cmd.go b/internal/cmd/serve/cmd.go index 0f7dc53..436d27f 100644 --- a/internal/cmd/serve/cmd.go +++ b/internal/cmd/serve/cmd.go @@ -21,6 +21,7 @@ import ( "github.com/fsnotify/fsnotify" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" + "github.com/rs/cors" "github.com/spf13/cobra" ) @@ -190,7 +191,12 @@ func watchFile(filename string, onChange func(in fsnotify.Event)) (*fsnotify.Wat func startServer(config *config.Config) error { postProcess(config) - server, err := gateway.StartHttpListener(config.Listen, http.NewServeMux()) + c := cors.New(cors.Options{ + AllowedOrigins: []string{"*"}, + }) + mux := http.NewServeMux() + handler := c.Handler(mux) + server, err := gateway.StartHttpListener(config.Listen, handler) if err != nil { return err }