Module.New() does not need to return an error

This commit is contained in:
Matthew Holt 2019-05-21 14:22:21 -06:00
parent 67d32e6779
commit 2fd98cb040
19 changed files with 29 additions and 36 deletions

View File

@ -42,11 +42,7 @@ func StartAdmin(addr string) error {
///// END PPROF STUFF ////// ///// END PPROF STUFF //////
for _, m := range GetModules("admin") { for _, m := range GetModules("admin") {
moduleValue, err := m.New() route := m.New().(AdminRoute)
if err != nil {
return fmt.Errorf("initializing module '%s': %v", m.Name, err)
}
route := moduleValue.(AdminRoute)
mux.Handle(route.Pattern, route) mux.Handle(route.Pattern, route)
} }

View File

@ -75,10 +75,7 @@ func (ctx Context) LoadModule(name string, rawMsg json.RawMessage) (interface{},
return nil, fmt.Errorf("module '%s' has no constructor", mod.Name) return nil, fmt.Errorf("module '%s' has no constructor", mod.Name)
} }
val, err := mod.New() val := mod.New()
if err != nil {
return nil, fmt.Errorf("initializing module '%s': %v", mod.Name, err)
}
// value must be a pointer for unmarshaling into concrete type // value must be a pointer for unmarshaling into concrete type
if rv := reflect.ValueOf(val); rv.Kind() != reflect.Ptr { if rv := reflect.ValueOf(val); rv.Kind() != reflect.Ptr {
@ -87,7 +84,7 @@ func (ctx Context) LoadModule(name string, rawMsg json.RawMessage) (interface{},
// fill in its config only if there is a config to fill in // fill in its config only if there is a config to fill in
if len(rawMsg) > 0 { if len(rawMsg) > 0 {
err = json.Unmarshal(rawMsg, &val) err := json.Unmarshal(rawMsg, &val)
if err != nil { if err != nil {
return nil, fmt.Errorf("decoding module config: %s: %v", mod.Name, err) return nil, fmt.Errorf("decoding module config: %s: %v", mod.Name, err)
} }

View File

@ -20,7 +20,7 @@ type Module struct {
// invoke methods on the returned value. // invoke methods on the returned value.
// It must return a pointer; if not, it // It must return a pointer; if not, it
// is converted into one. // is converted into one.
New func() (interface{}, error) New func() interface{}
} }
func (m Module) String() string { return m.Name } func (m Module) String() string { return m.Name }

View File

@ -22,7 +22,7 @@ func init() {
err := caddy2.RegisterModule(caddy2.Module{ err := caddy2.RegisterModule(caddy2.Module{
Name: "http", Name: "http",
New: func() (interface{}, error) { return new(App), nil }, New: func() interface{} { return new(App) },
}) })
if err != nil { if err != nil {

View File

@ -12,7 +12,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.middleware.log", Name: "http.middleware.log",
New: func() (interface{}, error) { return new(Log), nil }, New: func() interface{} { return new(Log) },
}) })
} }

View File

@ -11,7 +11,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.file", Name: "http.matchers.file",
New: func() (interface{}, error) { return new(FileMatcher), nil }, New: func() interface{} { return new(FileMatcher) },
}) })
} }

View File

@ -21,7 +21,7 @@ func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.responders.file_server", Name: "http.responders.file_server",
New: func() (interface{}, error) { return new(FileServer), nil }, New: func() interface{} { return new(FileServer) },
}) })
} }

View File

@ -11,7 +11,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.middleware.headers", Name: "http.middleware.headers",
New: func() (interface{}, error) { return new(Headers), nil }, New: func() interface{} { return new(Headers) },
}) })
} }

View File

@ -32,39 +32,39 @@ type (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.host", Name: "http.matchers.host",
New: func() (interface{}, error) { return matchHost{}, nil }, New: func() interface{} { return matchHost{} },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.path", Name: "http.matchers.path",
New: func() (interface{}, error) { return matchPath{}, nil }, New: func() interface{} { return matchPath{} },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.path_regexp", Name: "http.matchers.path_regexp",
New: func() (interface{}, error) { return new(matchPathRE), nil }, New: func() interface{} { return new(matchPathRE) },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.method", Name: "http.matchers.method",
New: func() (interface{}, error) { return matchMethod{}, nil }, New: func() interface{} { return matchMethod{} },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.query", Name: "http.matchers.query",
New: func() (interface{}, error) { return matchQuery{}, nil }, New: func() interface{} { return matchQuery{} },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.header", Name: "http.matchers.header",
New: func() (interface{}, error) { return matchHeader{}, nil }, New: func() interface{} { return matchHeader{} },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.header_regexp", Name: "http.matchers.header_regexp",
New: func() (interface{}, error) { return matchHeaderRE{}, nil }, New: func() interface{} { return matchHeaderRE{} },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.protocol", Name: "http.matchers.protocol",
New: func() (interface{}, error) { return new(matchProtocol), nil }, New: func() interface{} { return new(matchProtocol) },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.starlark_expr", Name: "http.matchers.starlark_expr",
New: func() (interface{}, error) { return new(matchStarlarkExpr), nil }, New: func() interface{} { return new(matchStarlarkExpr) },
}) })
} }

View File

@ -8,6 +8,6 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.responders.reverse_proxy", Name: "http.responders.reverse_proxy",
New: func() (interface{}, error) { return new(LoadBalanced), nil }, New: func() interface{} { return new(LoadBalanced) },
}) })
} }

View File

@ -12,7 +12,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.middleware.rewrite", Name: "http.middleware.rewrite",
New: func() (interface{}, error) { return new(Rewrite), nil }, New: func() interface{} { return new(Rewrite) },
}) })
} }

View File

@ -11,7 +11,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.responders.static", Name: "http.responders.static",
New: func() (interface{}, error) { return new(Static), nil }, New: func() interface{} { return new(Static) },
}) })
} }

View File

@ -9,12 +9,12 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.middleware.table", Name: "http.middleware.table",
New: func() (interface{}, error) { return new(tableMiddleware), nil }, New: func() interface{} { return new(tableMiddleware) },
}) })
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "http.matchers.table", Name: "http.matchers.table",
New: func() (interface{}, error) { return new(tableMatcher), nil }, New: func() interface{} { return new(tableMatcher) },
}) })
} }

View File

@ -15,7 +15,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "tls.management.acme", Name: "tls.management.acme",
New: func() (interface{}, error) { return new(acmeManagerMaker), nil }, New: func() interface{} { return new(acmeManagerMaker) },
}) })
} }

View File

@ -11,7 +11,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "tls.certificates.load_files", Name: "tls.certificates.load_files",
New: func() (interface{}, error) { return fileLoader{}, nil }, New: func() interface{} { return fileLoader{} },
}) })
} }

View File

@ -16,7 +16,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "tls.certificates.load_folders", Name: "tls.certificates.load_folders",
New: func() (interface{}, error) { return folderLoader{}, nil }, New: func() interface{} { return folderLoader{} },
}) })
} }

View File

@ -12,7 +12,7 @@ type MatchServerName []string
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "tls.handshake_match.host", Name: "tls.handshake_match.host",
New: func() (interface{}, error) { return MatchServerName{}, nil }, New: func() interface{} { return MatchServerName{} },
}) })
} }

View File

@ -15,7 +15,7 @@ import (
func init() { func init() {
caddy2.RegisterModule(caddy2.Module{ caddy2.RegisterModule(caddy2.Module{
Name: "tls", Name: "tls",
New: func() (interface{}, error) { return new(TLS), nil }, New: func() interface{} { return new(TLS) },
}) })
} }

View File

@ -11,7 +11,7 @@ import (
func init() { func init() {
RegisterModule(Module{ RegisterModule(Module{
Name: "caddy.storage.file_system", Name: "caddy.storage.file_system",
New: func() (interface{}, error) { return new(fileStorage), nil }, New: func() interface{} { return new(fileStorage) },
}) })
} }