From d030bfdae065c8776e8ac14c0a28f725ec05145c Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 16 Sep 2019 11:04:18 -0600 Subject: [PATCH] httpcaddyfile: static_response -> respond; minor cleanups --- caddyconfig/caddyfile/parse_test.go | 2 -- caddyconfig/httpcaddyfile/builtins.go | 10 ++++++++++ caddyconfig/httpcaddyfile/directives.go | 3 +-- modules/caddyhttp/encode/caddyfile.go | 3 --- modules/caddyhttp/staticresp.go | 10 ++++++---- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/caddyconfig/caddyfile/parse_test.go b/caddyconfig/caddyfile/parse_test.go index d4e22cc4..0168cfa5 100755 --- a/caddyconfig/caddyfile/parse_test.go +++ b/caddyconfig/caddyfile/parse_test.go @@ -22,8 +22,6 @@ import ( "testing" ) -// TODO: re-enable all tests - func TestAllTokens(t *testing.T) { input := strings.NewReader("a b c\nd e") expected := []string{"a", "b", "c", "d", "e"} diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go index 0ac436e1..ff93b115 100644 --- a/caddyconfig/httpcaddyfile/builtins.go +++ b/caddyconfig/httpcaddyfile/builtins.go @@ -31,6 +31,7 @@ func init() { RegisterDirective("root", parseRoot) RegisterDirective("tls", parseTLS) RegisterHandlerDirective("redir", parseRedir) + RegisterHandlerDirective("respond", parseRespond) } func parseBind(h Helper) ([]ConfigValue, error) { @@ -253,3 +254,12 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) { Body: body, }, nil } + +func parseRespond(h Helper) (caddyhttp.MiddlewareHandler, error) { + sr := new(caddyhttp.StaticResponse) + err := sr.UnmarshalCaddyfile(h.Dispenser) + if err != nil { + return nil, err + } + return sr, nil +} diff --git a/caddyconfig/httpcaddyfile/directives.go b/caddyconfig/httpcaddyfile/directives.go index 14c5f444..2f89f6db 100644 --- a/caddyconfig/httpcaddyfile/directives.go +++ b/caddyconfig/httpcaddyfile/directives.go @@ -25,7 +25,6 @@ import ( // defaultDirectiveOrder specifies the order // to apply directives in HTTP routes. -// TODO: finish the ability to customize this var defaultDirectiveOrder = []string{ "rewrite", "try_files", @@ -34,7 +33,7 @@ var defaultDirectiveOrder = []string{ "encode", "templates", "redir", - "static_response", // TODO: "reply" or "respond"? + "respond", "reverse_proxy", "php_fastcgi", "file_server", diff --git a/modules/caddyhttp/encode/caddyfile.go b/modules/caddyhttp/encode/caddyfile.go index d23eab9a..2f0e1512 100644 --- a/modules/caddyhttp/encode/caddyfile.go +++ b/modules/caddyhttp/encode/caddyfile.go @@ -29,7 +29,6 @@ func init() { httpcaddyfile.RegisterHandlerDirective("encode", parseCaddyfile) } -// TODO: This is a good example of why UnmarshalCaddyfile is still a good idea... hmm. func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { enc := new(Encode) err := enc.UnmarshalCaddyfile(h.Dispenser) @@ -39,8 +38,6 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) return enc, nil } -// TODO: Keep UnmarshalCaddyfile pattern? - // UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax: // // encode [] { diff --git a/modules/caddyhttp/staticresp.go b/modules/caddyhttp/staticresp.go index 21ff9d5b..a435f872 100644 --- a/modules/caddyhttp/staticresp.go +++ b/modules/caddyhttp/staticresp.go @@ -25,7 +25,6 @@ import ( func init() { caddy.RegisterModule(StaticResponse{}) - // TODO: Caddyfile directive } // StaticResponse implements a simple responder for static responses. @@ -46,7 +45,7 @@ func (StaticResponse) CaddyModule() caddy.ModuleInfo { // UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax: // -// static_response [] { +// respond [] { // body // close // } @@ -119,5 +118,8 @@ func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Hand return nil } -// Interface guard -var _ MiddlewareHandler = (*StaticResponse)(nil) +// Interface guards +var ( + _ MiddlewareHandler = (*StaticResponse)(nil) + _ caddyfile.Unmarshaler = (*StaticResponse)(nil) +)