Added cors

This commit is contained in:
SwiftyOS
2024-09-12 18:19:18 +02:00
parent 004e49edb1
commit 220a127e51
5 changed files with 17 additions and 0 deletions

View File

@@ -2,3 +2,6 @@ ServerAddress: ":8015"
DatabaseURL: "postgresql://agpt_user:pass123@localhost:5432/agpt_local?connect_timeout=60"
JWTSecret: "Z86RsQ+nhSk+A8ODJX1kQA11JCk9nlw8n+MRdSgmR+P1sMPTTDG1rjBTwj7Ucjb3TRHSVxkCNPgXISmzU/vMkA=="
JWTAlgorithm: "HS256"
CORSAllowOrigins:
- "http://localhost:3000"
- "http://127.0.0.1:3000"

View File

@@ -13,6 +13,7 @@ type Config struct {
AuthEnabled bool `mapstructure:"authenabled"`
JWTSecret string `mapstructure:"jwtsecret"`
JWTAlgorithm string `mapstructure:"jwtalgorithm"`
CORSAllowOrigins []string `mapstructure:"corsalloworigins"`
}
func Load(configFile ...string) (*Config, error) {

View File

@@ -25,6 +25,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // 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-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect

View File

@@ -23,6 +23,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
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/gzip v1.0.1 h1:HQ8ENHODeLY7a4g1Au/46Z92bdGFl74OhxcZble9WJE=
github.com/gin-contrib/gzip v1.0.1/go.mod h1:njt428fdUNRvjuJf16tZMYZ2Yl+WQB53X5wmhDwXvC4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=

View File

@@ -6,6 +6,7 @@ import (
"time"
"github.com/Depado/ginprom"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"github.com/swiftyos/market/config"
@@ -50,6 +51,15 @@ func main() {
r.Use(ginzap.RecoveryWithZap(logger, true))
r.Use(middleware.Gzip())
// Update CORS configuration
corsConfig := cors.DefaultConfig()
if len(cfg.CORSAllowOrigins) > 0 {
corsConfig.AllowOrigins = cfg.CORSAllowOrigins
} else {
corsConfig.AllowOrigins = []string{"*"} // Fallback to allow all origins if not specified
}
r.Use(cors.New(corsConfig))
// Route welcome
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Welcome to the Marketplace API")