From 185f78df62a01d886e6cc4c60eb7831911ca8405 Mon Sep 17 00:00:00 2001 From: Mukul Mantosh Date: Mon, 19 Aug 2024 19:05:43 +0530 Subject: [PATCH] Add CORS support and URL encoding for Unsplash API Integrated CORS middleware into Gin engine and configured essential CORS settings. Updated Unsplash API request to escape query parameters, ensuring proper handling of special characters in menu items. --- go.mod | 1 + go.sum | 2 ++ pkg/handler/server.go | 12 +++++++++++- pkg/service/restaurant/unsplash/image.go | 5 +++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 125d86c..19c1f7b 100644 --- a/go.mod +++ b/go.mod @@ -42,6 +42,7 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/gabriel-vasile/mimetype v1.4.5 // indirect + github.com/gin-contrib/cors v1.7.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect diff --git a/go.sum b/go.sum index 2a7f227..d300d02 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2 github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= +github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw= +github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= diff --git a/pkg/handler/server.go b/pkg/handler/server.go index 69b6648..a3663ff 100644 --- a/pkg/handler/server.go +++ b/pkg/handler/server.go @@ -3,6 +3,7 @@ package handler import ( "Go_Food_Delivery/pkg/database" "Go_Food_Delivery/pkg/storage" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" sloggin "github.com/samber/slog-gin" "log/slog" @@ -20,9 +21,18 @@ func NewServer(db database.Database) *Server { ginEngine := gin.New() - // Setting Logger & MultipartMemory + // CORS configuration + corsConfig := cors.Config{ + AllowOrigins: []string{"*"}, // List of allowed origins + AllowMethods: []string{"GET", "POST", "PUT", "DELETE"}, // List of allowed methods + + AllowCredentials: true, + } + + // Setting Logger, CORS & MultipartMemory ginEngine.Use(sloggin.New(logger)) ginEngine.Use(gin.Recovery()) + ginEngine.Use(cors.New(corsConfig)) ginEngine.MaxMultipartMemory = 8 << 20 // 8 MB localStoragePath := os.Getenv("LOCAL_STORAGE_PATH") diff --git a/pkg/service/restaurant/unsplash/image.go b/pkg/service/restaurant/unsplash/image.go index 6d4edc5..0a90b22 100644 --- a/pkg/service/restaurant/unsplash/image.go +++ b/pkg/service/restaurant/unsplash/image.go @@ -6,13 +6,14 @@ import ( "io" "log" "net/http" + "net/url" "os" ) func GetUnSplashImageURL(menuItem string) string { - url := "https://api.unsplash.com/search/photos/?page=1&query=" + menuItem + "&w=400&h=400" - req, err := http.NewRequest("GET", url, nil) + imageUrl := "https://api.unsplash.com/search/photos/?page=1&query=" + url.QueryEscape(menuItem) + "&w=400&h=400" + req, err := http.NewRequest("GET", imageUrl, nil) if err != nil { log.Fatalf("Failed to create request: %v", err) }