From a9463896d4186b3e2bebf95f0ecd7785fa24aa40 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 13 Apr 2023 23:33:48 +0200 Subject: [PATCH] feat(backend): add cors --- backend/server/server.go | 10 ++++++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 13 insertions(+) diff --git a/backend/server/server.go b/backend/server/server.go index 70e9725..459832d 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -11,6 +11,7 @@ import ( "codeberg.org/u1f320/pronouns.cc/backend/server/rate" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/cors" "github.com/go-chi/httprate" "github.com/go-chi/render" ) @@ -48,6 +49,15 @@ func New() (*Server, error) { s.Router.Use(middleware.Logger) } s.Router.Use(middleware.Recoverer) + // add CORS + s.Router.Use(cors.Handler(cors.Options{ + AllowedOrigins: []string{"https://*", "http://*"}, + AllowedMethods: []string{"HEAD", "GET"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"}, + AllowCredentials: false, + MaxAge: 300, + })) + // enable authentication for all routes (but don't require it) s.Router.Use(s.maybeAuth) diff --git a/go.mod b/go.mod index 9287117..994a3a2 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/dustin/go-humanize v1.0.1 // indirect + github.com/go-chi/cors v1.2.1 // indirect github.com/go-gorp/gorp/v3 v3.1.0 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.3.0 // indirect diff --git a/go.sum b/go.sum index 585e2d5..2f62af8 100644 --- a/go.sum +++ b/go.sum @@ -120,6 +120,8 @@ github.com/georgysavva/scany/v2 v2.0.0/go.mod h1:sigOdh+0qb/+aOs3TVhehVT10p8qJL7 github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 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-chi/httprate v0.7.1 h1:d5kXARdms2PREQfU4pHvq44S6hJ1hPu4OXLeBKmCKWs= github.com/go-chi/httprate v0.7.1/go.mod h1:6GOYBSwnpra4CQfAKXu8sQZg+nZ0M1g9QnyFvxrAB8A= github.com/go-chi/render v1.0.2 h1:4ER/udB0+fMWB2Jlf15RV3F4A2FDuYi/9f+lFttR/Lg=