Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
Add Validator interface
Modules can now verify their own configurations
Matthew Holt committed
Apr 3, 2019 at 11:41 UTC
3eae6d43b62db96c1d69e82c8d2997b6bf41beda
1 file changed
+16
modules.go
+16
@@ -138,6 +138,13 @@ func LoadModule(name string, rawMsg json.RawMessage) (interface{}, error) {
138
return nil, fmt.Errorf("decoding module config: %s: %v", mod.Name, err)
139
}
140
141
+ if validator, ok := val.(Validator); ok {
142
+ err := validator.Validate()
143
+ if err != nil {
144
+ return nil, fmt.Errorf("%s: invalid configuration: %v", mod.Name, err)
145
+ }
146
+ }
147
+
148
return val, nil
149
}
150
@@ -170,6 +177,15 @@ func LoadModuleInlineName(moduleScope string, raw json.RawMessage) (interface{},
177
return val, nil
178
}
179
180
+// Validator is implemented by modules which can verify that their
181
+// configurations are valid. This method will be called after New()
182
+// instantiations of modules (if implemented). Validation should
183
+// always be fast (imperceptible running time) and an error should
184
+// be returned only if the value's configuration is invalid.
185
+type Validator interface {
186
+ Validate() error
187
+}
188
+
189
var (
190
modules = make(map[string]Module)
191
modulesMu sync.Mutex