Skip to content

Commit

Permalink
Merge pull request #16 from SmilyOrg:cors-fix
Browse files Browse the repository at this point in the history
Stricter CORS handling
  • Loading branch information
SmilyOrg authored Aug 15, 2022
2 parents 9dbf0de + a9ccac8 commit 6f49f3d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PHOTOFIELD_DATA_DIR=./data
PHOTOFIELD_API_PREFIX=/
PHOTOFIELD_CORS_ALLOWED_ORIGINS=http://localhost:3000
15 changes: 9 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,12 +1031,15 @@ func main() {

r.Route(apiPrefix, func(r chi.Router) {

r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
allowedOrigins := os.Getenv("PHOTOFIELD_CORS_ALLOWED_ORIGINS")
if allowedOrigins != "" {
r.Use(cors.Handler(cors.Options{
AllowedOrigins: strings.Split(allowedOrigins, ","),
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
}

var api Api
r.Mount("/", openapi.Handler(&api))
Expand Down

0 comments on commit 6f49f3d

Please sign in to comment.