mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
Add Validator interface
Modules can now verify their own configurations
This commit is contained in:
parent
59a5d0db28
commit
3eae6d43b6
1 changed files with 16 additions and 0 deletions
16
modules.go
16
modules.go
|
@ -138,6 +138,13 @@ func LoadModule(name string, rawMsg json.RawMessage) (interface{}, error) {
|
||||||
return nil, fmt.Errorf("decoding module config: %s: %v", mod.Name, err)
|
return nil, fmt.Errorf("decoding module config: %s: %v", mod.Name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if validator, ok := val.(Validator); ok {
|
||||||
|
err := validator.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s: invalid configuration: %v", mod.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return val, nil
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,6 +177,15 @@ func LoadModuleInlineName(moduleScope string, raw json.RawMessage) (interface{},
|
||||||
return val, nil
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validator is implemented by modules which can verify that their
|
||||||
|
// configurations are valid. This method will be called after New()
|
||||||
|
// instantiations of modules (if implemented). Validation should
|
||||||
|
// always be fast (imperceptible running time) and an error should
|
||||||
|
// be returned only if the value's configuration is invalid.
|
||||||
|
type Validator interface {
|
||||||
|
Validate() error
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
modules = make(map[string]Module)
|
modules = make(map[string]Module)
|
||||||
modulesMu sync.Mutex
|
modulesMu sync.Mutex
|
||||||
|
|
Loading…
Reference in a new issue