Allow override of contentType response from watchdog. (#46)

This commit is contained in:
Alex Ellis 2017-04-10 22:18:37 +01:00 committed by GitHub
parent c7db0faab4
commit f958e99bdc
4 changed files with 29 additions and 5 deletions

View File

@ -53,6 +53,7 @@ A number of environmental overrides can be added for additional flexibility and
|------------------------|--------------|
| `fprocess` | The process to invoke for each function call. This must be a UNIX binary and accept input via STDIN and output via STDOUT. |
| `marshal_requests` | Instead of re-directing the raw HTTP body into your fprocess, it will first be marshalled into JSON. Use this if you need to work with HTTP headers |
| `content_type` | Force a specific Content-Type response for all responses. |
| `write_timeout` | HTTP timeout for writing a response body from your function |
| `read_timeout` | HTTP timeout for reading the payload from the client caller |
| `suppress_lock` | The watchdog will attempt to write a lockfile to /tmp/ for swarm healthchecks - set this to true to disable behaviour. |
@ -66,8 +67,7 @@ By default the watchdog will match the response of your function to the "Content
* If your client sends a JSON post with a Content-Type of `application/json` this will be matched automatically in the response.
* If your client sends a JSON post with a Content-Type of `text/plain` this will be matched automatically in the response too
See open issues for custom override of response ContentType.
To override the Content-Type of all your responses set the `content_type` environmental variable.
**Tuning auto-scaling**

View File

@ -73,6 +73,19 @@ func TestRead_SuppressLockConfig(t *testing.T) {
}
}
func TestRead_ContentTypeConfig(t *testing.T) {
defaults := NewEnvBucket()
readConfig := ReadConfig{}
defaults.Setenv("content_type", "application/json")
config := readConfig.Read(defaults)
if config.contentType != "application/json" {
t.Logf("content_type envVariable incorrect, got: %s.\n", config.contentType)
t.Fail()
}
}
func TestRead_FprocessConfig(t *testing.T) {
defaults := NewEnvBucket()
readConfig := ReadConfig{}

View File

@ -89,9 +89,15 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request)
os.Stdout.Write(out)
}
clientContentType := r.Header.Get("Content-Type")
if len(clientContentType) > 0 {
w.Header().Set("Content-Type", "application/json")
if len(config.contentType) > 0 {
w.Header().Set("Content-Type", config.contentType)
} else {
// Match content-type of caller if no override specified.
clientContentType := r.Header.Get("Content-Type")
if len(clientContentType) > 0 {
w.Header().Set("Content-Type", clientContentType)
}
}
w.WriteHeader(200)

View File

@ -64,6 +64,8 @@ func (ReadConfig) Read(hasEnv HasEnv) WatchdogConfig {
cfg.suppressLock = parseBoolValue(hasEnv.Getenv("suppress_lock"))
cfg.contentType = hasEnv.Getenv("content_type")
return cfg
}
@ -85,4 +87,7 @@ type WatchdogConfig struct {
// Don't write a lock file to /tmp/
suppressLock bool
// contentType forces a specific pre-defined value for all responses
contentType string
}