From 9e78692345d4d1e55348a94b68956c0d3311bf91 Mon Sep 17 00:00:00 2001 From: yyasha Date: Wed, 31 Jan 2024 10:18:29 +0300 Subject: [PATCH] add CORS; change get to post for json --- go.mod | 1 + go.sum | 2 ++ internal/app/httpSrv/httpSrv.go | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index edb16b1..72f41b0 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/go-chi/cors v1.2.1 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect diff --git a/go.sum b/go.sum index 64fa991..077f0d0 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= github.com/go-chi/chi/v5 v5.0.11/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/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4= github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= diff --git a/internal/app/httpSrv/httpSrv.go b/internal/app/httpSrv/httpSrv.go index a106994..2d374d1 100644 --- a/internal/app/httpSrv/httpSrv.go +++ b/internal/app/httpSrv/httpSrv.go @@ -15,6 +15,7 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/cors" ) type App struct { @@ -40,10 +41,19 @@ func New(log *slog.Logger, cfg *config.HTTPServerConfig, storage Storage, mediaS router.Use(middleware.RequestID) router.Use(mwLogger.New(log)) router.Use(middleware.Recoverer) + // Basic CORS + router.Use(cors.Handler(cors.Options{ + AllowedOrigins: []string{"https://*", "http://*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: false, + MaxAge: 300, // Maximum value not ignored by any of major browsers + })) // routes - router.Get("/recipe", recipe.New(log, storage)) - router.Get("/recipes_page", recipes.New(log, storage)) - router.Get("/recipes_by_category", recipes_by_category.New(log, storage)) + router.Post("/recipe", recipe.New(log, storage)) + router.Post("/recipes_page", recipes.New(log, storage)) + router.Post("/recipes_by_category", recipes_by_category.New(log, storage)) router.Get("/recipe_img", recipeimage.New(log, mediaStorage)) // server config srv := &http.Server{