Skip to content

Commit

Permalink
feat: customize allow origins, headers and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Nov 24, 2023
1 parent 6100647 commit 3f405de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 12 additions & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ type TasksConfig struct {
Copy TaskConfig `json:"copy" envPrefix:"COPY_"`
}

type Cors struct {
AllowOrigins []string `json:"allow_origins" env:"ALLOW_ORIGINS"`
AllowMethods []string `json:"allow_methods" env:"ALLOW_METHODS"`
AllowHeaders []string `json:"allow_headers" env:"ALLOW_HEADERS"`
}

type Config struct {
Force bool `json:"force" env:"FORCE"`
SiteURL string `json:"site_url" env:"SITE_URL"`
Expand All @@ -67,6 +73,7 @@ type Config struct {
MaxConnections int `json:"max_connections" env:"MAX_CONNECTIONS"`
TlsInsecureSkipVerify bool `json:"tls_insecure_skip_verify" env:"TLS_INSECURE_SKIP_VERIFY"`
Tasks TasksConfig `json:"tasks" envPrefix:"TASKS_"`
Cors Cors `json:"cors" envPrefix:"CORS_"`
}

func DefaultConfig() *Config {
Expand Down Expand Up @@ -120,5 +127,10 @@ func DefaultConfig() *Config {
MaxRetry: 2,
},
},
Cors: Cors{
AllowOrigins: []string{"*"},
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
},
}
}
7 changes: 4 additions & 3 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ func _fs(g *gin.RouterGroup) {

func Cors(r *gin.Engine) {
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AllowHeaders = []string{"*"}
config.AllowMethods = []string{"*"}
//config.AllowAllOrigins = true
config.AllowOrigins = conf.Conf.Cors.AllowOrigins
config.AllowHeaders = conf.Conf.Cors.AllowHeaders
config.AllowMethods = conf.Conf.Cors.AllowMethods
r.Use(cors.New(config))
}

0 comments on commit 3f405de

Please sign in to comment.