Skip to content

Commit

Permalink
Added middleware component for CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan923 committed Jan 26, 2024
1 parent dd9c92b commit e40c2ad
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backend/src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"github.com/Stefan923/go-estate-market/api/middleware"
"github.com/Stefan923/go-estate-market/api/router"
validator2 "github.com/Stefan923/go-estate-market/api/validator"
"github.com/Stefan923/go-estate-market/config"
Expand All @@ -15,6 +16,8 @@ func StartServer(config *config.Config) {
gin.SetMode(config.Server.RunningMode)
engine := gin.New()

engine.Use(middleware.CreateCorsMiddleware(config))

registerRoutes(engine, config)
registerValidators()

Expand Down
23 changes: 23 additions & 0 deletions backend/src/api/middleware/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package middleware

import (
"github.com/Stefan923/go-estate-market/config"
"github.com/gin-gonic/gin"
)

func CreateCorsMiddleware(cfg *config.Config) gin.HandlerFunc {
return func(context *gin.Context) {
context.Writer.Header().Set("Access-Control-Allow-Origin", cfg.Server.Cors.AllowedOrigins)
context.Header("Access-Control-Allow-Credentials", cfg.Server.Cors.AllowCredentials)
context.Header("Access-Control-Allow-Headers", cfg.Server.Cors.AllowedHeaders)
context.Header("Access-Control-Allow-Methods", cfg.Server.Cors.AllowedMethods)
context.Header("Access-Control-Max-Age", cfg.Server.Cors.MaxAge)
context.Set("content-type", cfg.Server.Cors.ContentType)
if context.Request.Method == "OPTIONS" {
context.AbortWithStatus(204)
return
}

context.Next()
}
}
10 changes: 10 additions & 0 deletions backend/src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ type ServerConfig struct {
RunningMode string
InternalPort string
ExternalPort string
Cors CorsConfig
}

type CorsConfig struct {
AllowedOrigins string
AllowedHeaders string
AllowedMethods string
AllowCredentials string
ContentType string
MaxAge string
}

func GetConfig() *Config {
Expand Down
9 changes: 8 additions & 1 deletion backend/src/config/development-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ auth:
server:
internalPort: 5000
externalPort: 5000
runningMode: debug
runningMode: debug
cors:
allowedOrigins: "*"
allowedHeaders: "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With"
allowedMethods: "POST , GET, OPTIONS, PUT, DELETE, UPDATE"
allowCredentials: "true"
contentType: "application/json"
maxAge: "21600"
9 changes: 8 additions & 1 deletion backend/src/config/docker-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ auth:
server:
internalPort: 5000
externalPort: 5000
runningMode: debug
runningMode: debug
cors:
allowedOrigins: "*"
allowedHeaders: "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With"
allowedMethods: "POST , GET, OPTIONS, PUT, DELETE, UPDATE"
allowCredentials: "true"
contentType: "application/json"
maxAge: "21600"

0 comments on commit e40c2ad

Please sign in to comment.