aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/encode
diff options
context:
space:
mode:
authorMatthew Holt <[email protected]>2019-08-21 10:46:35 -0600
committerMatthew Holt <[email protected]>2019-08-21 10:46:35 -0600
commitc9980fd3671d873a7197a5ac4d6ac9d6b046abb6 (patch)
tree75c301ab10590fb5f7d5b869a3424b8d46176bbf /modules/caddyhttp/encode
parentc4159ef76d279d6a84257b24dbe97430af32eb1e (diff)
downloadcaddy-c9980fd3671d873a7197a5ac4d6ac9d6b046abb6.tar.gz
caddy-c9980fd3671d873a7197a5ac4d6ac9d6b046abb6.zip
Refactor Caddyfile adapter and module registration
Use piles from which to draw config values. Module values can return their name, so now we can do two-way mapping from value to name and name to value; whereas before we could only map name to value. This was problematic with the Caddyfile adapter since it receives values and needs to know the name to put in the config.
Diffstat (limited to 'modules/caddyhttp/encode')
-rw-r--r--modules/caddyhttp/encode/brotli/brotli.go15
-rw-r--r--modules/caddyhttp/encode/caddyfile.go22
-rw-r--r--modules/caddyhttp/encode/encode.go13
-rw-r--r--modules/caddyhttp/encode/gzip/gzip.go15
-rw-r--r--modules/caddyhttp/encode/zstd/zstd.go15
5 files changed, 57 insertions, 23 deletions
diff --git a/modules/caddyhttp/encode/brotli/brotli.go b/modules/caddyhttp/encode/brotli/brotli.go
index 0a9f871ac..cf055aaf0 100644
--- a/modules/caddyhttp/encode/brotli/brotli.go
+++ b/modules/caddyhttp/encode/brotli/brotli.go
@@ -19,16 +19,13 @@ import (
"strconv"
"github.com/andybalholm/brotli"
- "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
)
func init() {
- caddy.RegisterModule(caddy.Module{
- Name: "http.encoders.brotli",
- New: func() interface{} { return new(Brotli) },
- })
+ caddy.RegisterModule(Brotli{})
}
// Brotli can create brotli encoders. Note that brotli
@@ -37,6 +34,14 @@ type Brotli struct {
Quality *int `json:"quality,omitempty"`
}
+// CaddyModule returns the Caddy module information.
+func (Brotli) CaddyModule() caddy.ModuleInfo {
+ return caddy.ModuleInfo{
+ Name: "http.encoders.brotli",
+ New: func() caddy.Module { return new(Brotli) },
+ }
+}
+
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens.
func (b *Brotli) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
diff --git a/modules/caddyhttp/encode/caddyfile.go b/modules/caddyhttp/encode/caddyfile.go
index 5aca6ac84..5762bd3b3 100644
--- a/modules/caddyhttp/encode/caddyfile.go
+++ b/modules/caddyhttp/encode/caddyfile.go
@@ -22,8 +22,25 @@ import (
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
+ "github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
+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)
+ if err != nil {
+ return nil, err
+ }
+ return enc, nil
+}
+
+// TODO: Keep UnmarshalCaddyfile pattern?
+
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
//
// encode [<matcher>] <formats...> {
@@ -78,8 +95,5 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return nil
}
-// Bucket returns the HTTP Caddyfile handler bucket number.
-func (enc Encode) Bucket() int { return 3 }
-
// Interface guard
-var _ httpcaddyfile.HandlerDirective = (*Encode)(nil)
+var _ caddyfile.Unmarshaler = (*Encode)(nil)
diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go
index 4e5f7437a..723b988a2 100644
--- a/modules/caddyhttp/encode/encode.go
+++ b/modules/caddyhttp/encode/encode.go
@@ -35,10 +35,7 @@ import (
)
func init() {
- caddy.RegisterModule(caddy.Module{
- Name: "http.handlers.encode",
- New: func() interface{} { return new(Encode) },
- })
+ caddy.RegisterModule(Encode{})
}
// Encode is a middleware which can encode responses.
@@ -50,6 +47,14 @@ type Encode struct {
writerPools map[string]*sync.Pool // TODO: these pools do not get reused through config reloads...
}
+// CaddyModule returns the Caddy module information.
+func (Encode) CaddyModule() caddy.ModuleInfo {
+ return caddy.ModuleInfo{
+ Name: "http.handlers.encode",
+ New: func() caddy.Module { return new(Encode) },
+ }
+}
+
// Provision provisions enc.
func (enc *Encode) Provision(ctx caddy.Context) error {
for modName, rawMsg := range enc.EncodingsRaw {
diff --git a/modules/caddyhttp/encode/gzip/gzip.go b/modules/caddyhttp/encode/gzip/gzip.go
index 87e8816a5..d6d67f716 100644
--- a/modules/caddyhttp/encode/gzip/gzip.go
+++ b/modules/caddyhttp/encode/gzip/gzip.go
@@ -20,16 +20,13 @@ import (
"fmt"
"strconv"
- "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
)
func init() {
- caddy.RegisterModule(caddy.Module{
- Name: "http.encoders.gzip",
- New: func() interface{} { return new(Gzip) },
- })
+ caddy.RegisterModule(Gzip{})
}
// Gzip can create gzip encoders.
@@ -37,6 +34,14 @@ type Gzip struct {
Level int `json:"level,omitempty"`
}
+// CaddyModule returns the Caddy module information.
+func (Gzip) CaddyModule() caddy.ModuleInfo {
+ return caddy.ModuleInfo{
+ Name: "http.encoders.gzip",
+ New: func() caddy.Module { return new(Gzip) },
+ }
+}
+
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens.
func (g *Gzip) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
diff --git a/modules/caddyhttp/encode/zstd/zstd.go b/modules/caddyhttp/encode/zstd/zstd.go
index 362262857..f2b4e8588 100644
--- a/modules/caddyhttp/encode/zstd/zstd.go
+++ b/modules/caddyhttp/encode/zstd/zstd.go
@@ -15,22 +15,27 @@
package caddyzstd
import (
- "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
"github.com/klauspost/compress/zstd"
)
func init() {
- caddy.RegisterModule(caddy.Module{
- Name: "http.encoders.zstd",
- New: func() interface{} { return new(Zstd) },
- })
+ caddy.RegisterModule(Zstd{})
}
// Zstd can create Zstandard encoders.
type Zstd struct{}
+// CaddyModule returns the Caddy module information.
+func (Zstd) CaddyModule() caddy.ModuleInfo {
+ return caddy.ModuleInfo{
+ Name: "http.encoders.zstd",
+ New: func() caddy.Module { return new(Zstd) },
+ }
+}
+
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens.
func (z *Zstd) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return nil