diff options
| author | Mohammed Al Sahaf <msaa1990@gmail.com> | 2020-03-22 01:49:10 +0300 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-21 16:49:10 -0600 | 
| commit | 37093befd59107316d422fcd3cc011b2481cd2cf (patch) | |
| tree | 2a55429f8f90b75874933192b19b71639c761a69 /caddyconfig/configadapters.go | |
| parent | d692d503a3d327d54c82bceab48bb1de07bb3c3d (diff) | |
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 <mholt@users.noreply.github.com>
Diffstat (limited to 'caddyconfig/configadapters.go')
| -rw-r--r-- | caddyconfig/configadapters.go | 21 | 
1 files changed, 20 insertions, 1 deletions
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)  | 
