Skip to content

Commit

Permalink
feat: CORS add support to allowed headers (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosz authored Mar 16, 2021
1 parent a5ca88b commit 0a7c6f7
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 33 deletions.
7 changes: 7 additions & 0 deletions examples/webshop/config/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ secret_key: supercalifajalistics
# characters (i.e.: http://*.domain.com).
cors_allowed_origins: ["*"]

# CORS: A list of headers the client is allowed to use with cross-domain
# requests. If the special "*" value is present in the list, all headers will be
# allowed. Default value is ["Origin", "Accept", "Content-Type",
# "X-Requested-With", "Authorization"]. Even if the list is empty, the "Origin"
# is always appended to the list.
cors_allowed_headers: []

# Debug Cross Origin Resource Sharing requests
cors_debug: false

Expand Down
7 changes: 7 additions & 0 deletions examples/webshop/config/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ reload_on_config_change: false
# characters (i.e.: http://*.domain.com).
# cors_allowed_origins: ["*"]

# CORS: A list of headers the client is allowed to use with cross-domain
# requests. If the special "*" value is present in the list, all headers will be
# allowed. Default value is ["Origin", "Accept", "Content-Type",
# "X-Requested-With", "Authorization"]. Even if the list is empty, the "Origin"
# is always appended to the list.
# cors_allowed_headers: []

# Debug Cross Origin Resource Sharing requests
# cors_debug: false

Expand Down
1 change: 1 addition & 0 deletions internal/serv/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Serv struct {
SeedFile string `mapstructure:"seed_file"`
MigrationsPath string `mapstructure:"migrations_path"`
AllowedOrigins []string `mapstructure:"cors_allowed_origins"`
AllowedHeaders []string `mapstructure:"cors_allowed_headers"`
DebugCORS bool `mapstructure:"cors_debug"`
APIPath string `mapstructure:"api_path"`
CacheControl string `mapstructure:"cache_control"`
Expand Down
5 changes: 5 additions & 0 deletions internal/serv/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ func apiV1Handler(sc *ServConfig) http.Handler {
}

if len(sc.conf.AllowedOrigins) != 0 {
allowedHeaders := []string{"Origin", "Accept", "Content-Type", "X-Requested-With", "Authorization"}
if len(sc.conf.AllowedHeaders) != 0 {
allowedHeaders = sc.conf.AllowedHeaders
}
c := cors.New(cors.Options{
AllowedOrigins: sc.conf.AllowedOrigins,
AllowedHeaders: allowedHeaders,
AllowCredentials: true,
Debug: sc.conf.DebugCORS,
})
Expand Down
66 changes: 33 additions & 33 deletions internal/serv/rice-box.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions internal/serv/tmpl/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ secret_key: supercalifajalistics
# characters (i.e.: http://*.domain.com).
cors_allowed_origins: ["*"]

# CORS: A list of headers the client is allowed to use with cross-domain
# requests. If the special "*" value is present in the list, all headers will be
# allowed. Default value is ["Origin", "Accept", "Content-Type",
# "X-Requested-With", "Authorization"]. Even if the list is empty, the "Origin"
# is always appended to the list.
cors_allowed_headers: []

# Debug Cross Origin Resource Sharing requests
cors_debug: false

Expand Down
7 changes: 7 additions & 0 deletions internal/serv/tmpl/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ reload_on_config_change: false
# characters (i.e.: http://*.domain.com).
# cors_allowed_origins: ["*"]

# CORS: A list of headers the client is allowed to use with cross-domain
# requests. If the special "*" value is present in the list, all headers will be
# allowed. Default value is ["Origin", "Accept", "Content-Type",
# "X-Requested-With", "Authorization"]. Even if the list is empty, the "Origin"
# is always appended to the list.
# cors_allowed_headers: []

# Debug Cross Origin Resource Sharing requests
# cors_debug: false

Expand Down

0 comments on commit 0a7c6f7

Please sign in to comment.