Skip to content

Commit

Permalink
chore: small CORS refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Valery Piashchynski <[email protected]>
  • Loading branch information
rustatian committed Jun 19, 2023
1 parent 0c74272 commit dc73b7a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4 # action page: <https://github.com/actions/setup-go>
with:
go-version: 1.19
go-version: stable

- name: Run linter
uses: golangci/[email protected] # Action page: <https://github.com/golangci/golangci-lint-action>
with:
version: v1.50 # without patch version
version: v1.53 # without patch version
only-new-issues: false # show only new issues if it's a pull request
args: --timeout=10m --build-tags=race
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
enable:
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
- bodyclose # Checks whether HTTP response body is closed successfully
- depguard # Go linter that checks if package imports are in a list of acceptable packages
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
- dupl # Tool for code clone detection
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
Expand Down
10 changes: 3 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@ type Config struct {
type CORSConfig struct {
// AllowedOrigin: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
AllowedOrigin string `mapstructure:"allowed_origin"`

// AllowedHeaders: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
AllowedHeaders string `mapstructure:"allowed_headers"`

// AllowedMethods: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
AllowedMethods string `mapstructure:"allowed_methods"`

// AllowCredentials https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
AllowCredentials *bool `mapstructure:"allow_credentials"`

AllowCredentials bool `mapstructure:"allow_credentials"`
// ExposeHeaders: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
ExposedHeaders string `mapstructure:"exposed_headers"`

// MaxAge of CORS headers in seconds/
MaxAge int `mapstructure:"max_age"`

// Status code to use for successful OPTIONS requests. Default value is http.StatusOK (200).
OptionsSuccessStatus int `mapstructure:"options_success_status"`
// Debug CORS requests
Debug bool `mapstructure:"debug"`
}
29 changes: 14 additions & 15 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ type Configurer interface {
// Plugin serves headers files. Potentially convert into middleware?
type Plugin struct {
// server configuration (location, forbidden files and etc)
cfg *Config

cfg *Config
prop propagation.TextMapPropagator

cors *cors.Options
cors *cors.Cors
}

// Init must return configure service and return true if service hasStatus enabled. Must return error in case of
Expand All @@ -60,38 +58,39 @@ func (p *Plugin) Init(cfg Configurer) error {

// Configure CORS options
if p.cfg.CORS != nil {
p.cors = &cors.Options{
opts := cors.Options{
// Keep BC with previous implementation
OptionsSuccessStatus: http.StatusOK,
Debug: p.cfg.CORS.Debug,
}

if p.cfg.CORS.AllowedOrigin != "" {
p.cors.AllowedOrigins = strings.Split(p.cfg.CORS.AllowedOrigin, ",")
opts.AllowedOrigins = strings.Split(p.cfg.CORS.AllowedOrigin, ",")
}

if p.cfg.CORS.AllowedMethods != "" {
p.cors.AllowedMethods = strings.Split(p.cfg.CORS.AllowedMethods, ",")
opts.AllowedMethods = strings.Split(p.cfg.CORS.AllowedMethods, ",")
}

if p.cfg.CORS.AllowedHeaders != "" {
p.cors.AllowedHeaders = strings.Split(p.cfg.CORS.AllowedHeaders, ",")
opts.AllowedHeaders = strings.Split(p.cfg.CORS.AllowedHeaders, ",")
}

if p.cfg.CORS.ExposedHeaders != "" {
p.cors.ExposedHeaders = strings.Split(p.cfg.CORS.ExposedHeaders, ",")
opts.ExposedHeaders = strings.Split(p.cfg.CORS.ExposedHeaders, ",")
}

if p.cfg.CORS.MaxAge > 0 {
p.cors.MaxAge = p.cfg.CORS.MaxAge
opts.MaxAge = p.cfg.CORS.MaxAge
}

if p.cfg.CORS.AllowCredentials != nil {
p.cors.AllowCredentials = *p.cfg.CORS.AllowCredentials
}
opts.AllowCredentials = p.cfg.CORS.AllowCredentials

if p.cfg.CORS.OptionsSuccessStatus != 0 {
p.cors.OptionsSuccessStatus = p.cfg.CORS.OptionsSuccessStatus
opts.OptionsSuccessStatus = p.cfg.CORS.OptionsSuccessStatus
}

p.cors = cors.New(opts)
}

return nil
Expand All @@ -101,7 +100,7 @@ func (p *Plugin) Init(cfg Configurer) error {
func (p *Plugin) Middleware(next http.Handler) http.Handler {
// Configure CORS handler
if p.cors != nil {
next = cors.New(*p.cors).Handler(next)
next = p.cors.Handler(next)
}

// Define the http.HandlerFunc
Expand Down

0 comments on commit dc73b7a

Please sign in to comment.