Skip to content

Commit

Permalink
feat: config cors (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSoZRious authored Dec 26, 2023
1 parent 777cba9 commit 032d016
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ REDIS_PASSWORD=123456

APP_PORT=3000
APP_ENV=development

CORS_ORIGINS=https://openhouse.chula.ac.th,https://isd-sgcu.in.th,https://openhouse.isd-sgcu.in.th
15 changes: 15 additions & 0 deletions cfgldr/cfgldr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Config struct {
DatabaseConfig DatabaseConfig
AppConfig AppConfig
RedisConfig RedisConfig
CorsConfig CorsConfig
}

type DatabaseConfig struct {
Expand All @@ -25,6 +26,10 @@ type RedisConfig struct {
Password string `mapstructure:"PASSWORD"`
}

type CorsConfig struct {
AllowOrigins string `mapstructure:"ORIGINS"`
}

func LoadConfig() (*Config, error) {
dbCfgLdr := viper.New()
dbCfgLdr.SetEnvPrefix("DB")
Expand Down Expand Up @@ -53,10 +58,20 @@ func LoadConfig() (*Config, error) {
return nil, err
}

corsConfigLdr := viper.New()
corsConfigLdr.SetEnvPrefix("CORS")
corsConfigLdr.AutomaticEnv()
dbCfgLdr.AllowEmptyEnv(false)
corsConfig := CorsConfig{}
if err := corsConfigLdr.Unmarshal(&corsConfig); err != nil {
return nil, err
}

return &Config{
DatabaseConfig: dbConfig,
AppConfig: appConfig,
RedisConfig: redisConfig,
CorsConfig: corsConfig,
}, nil
}

Expand Down
40 changes: 40 additions & 0 deletions cfgldr/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cfgldr

import (
"strings"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

type CorsHandler gin.HandlerFunc

func makeCorsConfig(cfg *Config) gin.HandlerFunc {
if cfg.AppConfig.IsDevelopment() {
return cors.New(cors.Config{
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
AllowOriginFunc: func(string) bool {
return true
},
})

}

allowOrigins := strings.Split(cfg.CorsConfig.AllowOrigins, ",")

return cors.New(cors.Config{
AllowOrigins: allowOrigins,
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
})
}

func MakeCorsConfig(cfg *Config) CorsHandler {
return CorsHandler(makeCorsConfig(cfg))
}
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func main() {
}
r := gin.Default()

r.Use(gin.HandlerFunc(container.CorsHandler))

r.GET("/_hc", container.HcHandler.HealthCheck)
r.GET("/live", container.FeatureflagHandler.GetLivestreamInfo)
r.GET("/events", container.EventHandler.GetAllEvents)
Expand Down
5 changes: 4 additions & 1 deletion di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@ type Container struct {
FeatureflagHandler featureflag.Handler
Config *cfgldr.Config
Logger *zap.Logger
CorsHandler cfgldr.CorsHandler
}

func newContainer(eventHandler event.Handler, hcHandler healthcheck.Handler, featureflagHandler featureflag.Handler, config *cfgldr.Config, logger *zap.Logger) Container {
func newContainer(eventHandler event.Handler, hcHandler healthcheck.Handler, featureflagHandler featureflag.Handler, config *cfgldr.Config, logger *zap.Logger, corsHandler cfgldr.CorsHandler) Container {
return Container{
eventHandler,
hcHandler,
featureflagHandler,
config,
logger,
corsHandler,
}
}

func Init() (Container, error) {
wire.Build(
newContainer,
cfgldr.LoadConfig,
cfgldr.MakeCorsConfig,
event.NewHandler,
event.NewService,
event.NewRepository,
Expand Down
8 changes: 5 additions & 3 deletions di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/isd-sgcu/oph66-backend
go 1.21.5

require (
github.com/gin-contrib/cors v1.5.0
github.com/gin-gonic/gin v1.9.1
github.com/google/wire v0.5.0
github.com/redis/go-redis/v9 v9.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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.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.5.0 h1:DgGKV7DDoOn36DFkNtbHrjoRiT5ExCe+PC9/xp7aKvk=
github.com/gin-contrib/cors v1.5.0/go.mod h1:TvU7MAZ3EwrPLI2ztzTt3tqgvBCq+wn8WpZmfADjupI=
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.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
Expand Down

0 comments on commit 032d016

Please sign in to comment.