From 37093befd59107316d422fcd3cc011b2481cd2cf Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Sun, 22 Mar 2020 01:49:10 +0300 Subject: caddyconfig: register adapters as Caddy modules (#3132) * admin: Refactor /load endpoint out of caddy package This eliminates the caddy package's dependency on the caddyconfig package, which helps prevent import cycles. * v2: adapter: register config adapters as Caddy modules * v2: adapter: simplify adapter registration as adapters and modules * v2: adapter: let RegisterAdapter be in charge of registering adapters as modules * v2: adapter: remove underscrores placeholders * v2: adapter: explicitly ignore the error of writing response of writing warnings back to client * Implicitly wrap config adapters as modules Co-authored-by: Matthew Holt --- caddyconfig/configadapters.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'caddyconfig/configadapters.go') diff --git a/caddyconfig/configadapters.go b/caddyconfig/configadapters.go index 071221f..96d7e10 100644 --- a/caddyconfig/configadapters.go +++ b/caddyconfig/configadapters.go @@ -17,6 +17,8 @@ package caddyconfig import ( "encoding/json" "fmt" + + "github.com/caddyserver/caddy/v2" ) // Adapter is a type which can adapt a configuration to Caddy JSON. @@ -105,7 +107,7 @@ func RegisterAdapter(name string, adapter Adapter) error { return fmt.Errorf("%s: already registered", name) } configAdapters[name] = adapter - return nil + return caddy.RegisterModule(adapterModule{name, adapter}) } // GetAdapter returns the adapter with the given name, @@ -114,4 +116,21 @@ func GetAdapter(name string) Adapter { return configAdapters[name] } +// adapterModule is a wrapper type that can turn any config +// adapter into a Caddy module, which has the benefit of being +// counted with other modules, even though they do not +// technically extend the Caddy configuration structure. +// See caddyserver/caddy#3132. +type adapterModule struct { + name string + Adapter +} + +func (am adapterModule) CaddyModule() caddy.ModuleInfo { + return caddy.ModuleInfo{ + ID: caddy.ModuleID("caddy.adapters." + am.name), + New: func() caddy.Module { return am }, + } +} + var configAdapters = make(map[string]Adapter) -- cgit v1.2.3