diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-07-09 12:58:39 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-07-09 12:58:39 -0600 |
commit | 4a3a418156e25aae17659142a4bf9259d7702c44 (patch) | |
tree | 559461c6504b7c1be38e0ddfbf0793b473ef8d66 /modules/caddyhttp/staticresp.go | |
parent | 6dfba5fda82e216cffb117a62fcefbe61cd25a34 (diff) |
Flatten HTTP handler config (#2662)
Differentiating middleware and responders has one benefit, namely that
it's clear which module provides the response, but even then it's not
a great advantage. Linear handler config makes a little more sense,
giving greater flexibility and simplifying the core a bit, even though
it's slightly awkward that handlers which are responders may not use
the 'next' handler that is passed in at all.
Diffstat (limited to 'modules/caddyhttp/staticresp.go')
-rw-r--r-- | modules/caddyhttp/staticresp.go | 24 |
1 files changed, 10 insertions, 14 deletions
diff --git a/modules/caddyhttp/staticresp.go b/modules/caddyhttp/staticresp.go index 3f43c92..8e4a3df 100644 --- a/modules/caddyhttp/staticresp.go +++ b/modules/caddyhttp/staticresp.go @@ -24,21 +24,20 @@ import ( func init() { caddy.RegisterModule(caddy.Module{ - Name: "http.responders.static", + Name: "http.handlers.static", New: func() interface{} { return new(Static) }, }) } // Static implements a simple responder for static responses. type Static struct { - StatusCode int `json:"status_code"` // TODO: should we turn this into a string so that only one field is needed? (string allows replacements) - StatusCodeStr string `json:"status_code_str"` - Headers http.Header `json:"headers"` - Body string `json:"body"` - Close bool `json:"close"` + StatusCode string `json:"status_code"` + Headers http.Header `json:"headers"` + Body string `json:"body"` + Close bool `json:"close"` } -func (s Static) ServeHTTP(w http.ResponseWriter, r *http.Request) error { +func (s Static) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error { repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer) // close the connection after responding @@ -60,16 +59,13 @@ func (s Static) ServeHTTP(w http.ResponseWriter, r *http.Request) error { } // get the status code - statusCode := s.StatusCode - if statusCode == 0 && s.StatusCodeStr != "" { - intVal, err := strconv.Atoi(repl.ReplaceAll(s.StatusCodeStr, "")) + statusCode := http.StatusOK + if s.StatusCode != "" { + intVal, err := strconv.Atoi(repl.ReplaceAll(s.StatusCode, "")) if err == nil { statusCode = intVal } } - if statusCode == 0 { - statusCode = http.StatusOK - } // write headers w.WriteHeader(statusCode) @@ -83,4 +79,4 @@ func (s Static) ServeHTTP(w http.ResponseWriter, r *http.Request) error { } // Interface guard -var _ Handler = (*Static)(nil) +var _ MiddlewareHandler = (*Static)(nil) |