Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restapi: Add configurable response headers #578

Merged
merged 2 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion api/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ const (
DefaultIdleTimeout = 120 * time.Second
)

// These are the default values for Config.
var (
DefaultHeaders = map[string][]string{
"Access-Control-Allow-Headers": []string{"X-Requested-With", "Range"},
"Access-Control-Allow-Methods": []string{"GET"},
"Access-Control-Allow-Origin": []string{"*"},
}
)

// Config is used to intialize the API object and allows to
// customize the behaviour of it. It implements the config.ComponentConfig
// interface.
Expand Down Expand Up @@ -71,6 +80,10 @@ type Config struct {
// BasicAuthCreds is a map of username-password pairs
// which are authorized to use Basic Authentication
BasicAuthCreds map[string]string

// Headers provides customization for the headers returned
// by the API. By default it sets a CORS policy.
Headers map[string][]string
}

type jsonConfig struct {
Expand All @@ -87,7 +100,8 @@ type jsonConfig struct {
ID string `json:"id,omitempty"`
PrivateKey string `json:"private_key,omitempty"`

BasicAuthCreds map[string]string `json:"basic_auth_credentials"`
BasicAuthCreds map[string]string `json:"basic_auth_credentials"`
Headers map[string][]string `json:"headers"`
}

// ConfigKey returns a human-friendly identifier for this type of
Expand Down Expand Up @@ -116,6 +130,9 @@ func (cfg *Config) Default() error {
// Auth
cfg.BasicAuthCreds = nil

// Headers
cfg.Headers = DefaultHeaders

return nil
}

Expand Down Expand Up @@ -177,6 +194,7 @@ func (cfg *Config) LoadJSON(raw []byte) error {

// Other options
cfg.BasicAuthCreds = jcfg.BasicAuthCreds
cfg.Headers = jcfg.Headers

return cfg.Validate()
}
Expand Down Expand Up @@ -295,6 +313,7 @@ func (cfg *Config) ToJSON() (raw []byte, err error) {
WriteTimeout: cfg.WriteTimeout.String(),
IdleTimeout: cfg.IdleTimeout.String(),
BasicAuthCreds: cfg.BasicAuthCreds,
Headers: cfg.Headers,
}

if cfg.ID != "" {
Expand Down