From c2b08fba5551a1e6f79c7c356b6ff6647a81e922 Mon Sep 17 00:00:00 2001 From: anGie44 Date: Sun, 5 Jun 2022 12:24:34 -0400 Subject: [PATCH] feat: allow origins --- go.mod | 2 ++ go.sum | 4 ++++ main.go | 6 +++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b7004e8..1a538d2 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/anGie44/theOffice-api go 1.18 require ( + github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 github.com/mxschmitt/golang-combinations v1.1.0 github.com/nicholasjackson/env v0.6.0 @@ -10,6 +11,7 @@ require ( ) require ( + github.com/felixge/httpsnoop v1.0.1 // indirect github.com/go-stack/stack v1.8.0 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/klauspost/compress v1.13.6 // indirect diff --git a/go.sum b/go.sum index 2d096a9..e68e869 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,16 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= +github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= +github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= diff --git a/main.go b/main.go index b3684a1..c43bf92 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/anGie44/theOffice-api/data" "github.com/anGie44/theOffice-api/handlers" + gohandlers "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/nicholasjackson/env" ) @@ -52,9 +53,12 @@ func main() { // V2 handlers to use request body for filtering data getRouter.HandleFunc("/v2/quotes", qh.GetQuotes) + // CORS + ch := gohandlers.CORS(gohandlers.AllowedOrigins([]string{"*"})) + s := http.Server{ Addr: bindAddress, - Handler: sm, + Handler: ch(sm), ErrorLog: l, ReadTimeout: 1 * time.Minute, IdleTimeout: 2 * time.Minute,