feat: cors origin

This commit is contained in:
Davidson Gomes 2024-10-22 07:36:30 -03:00
parent 3021043836
commit 82895f17c1
4 changed files with 20 additions and 0 deletions

View File

@ -1,2 +1,3 @@
PORT=4040
CORS_ALLOW_ORIGINS=*
API_KEY=429683C4C977415CAAFCCE10F7D57E11

1
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/cors v1.7.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect

2
go.sum
View File

@ -11,6 +11,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
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=

16
main.go
View File

@ -14,6 +14,7 @@ import (
"strings"
"sync"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
@ -160,8 +161,23 @@ func main() {
port = "8080"
}
allowOriginsEnv := os.Getenv("CORS_ALLOW_ORIGINS")
var allowOrigins []string
if allowOriginsEnv != "" {
allowOrigins = strings.Split(allowOriginsEnv, ",")
} else {
allowOrigins = []string{"*"}
}
router := gin.Default()
config := cors.DefaultConfig()
config.AllowOrigins = allowOrigins
config.AllowMethods = []string{"POST", "GET", "OPTIONS"}
config.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization", "apikey"}
router.Use(cors.New(config))
router.POST("/process-audio", processAudio)
router.Run(":" + port)