Skip to content

Commit

Permalink
Add CORS support and URL encoding for Unsplash API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mukulmantosh committed Aug 19, 2024
1 parent 226ae6d commit 185f78d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 11 additions & 1 deletion pkg/handler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions pkg/service/restaurant/unsplash/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 185f78d

Please sign in to comment.