diff --git a/admin.go b/admin.go index 8cbf8fb2..b0d12992 100644 --- a/admin.go +++ b/admin.go @@ -34,7 +34,6 @@ import ( "net/url" "os" "path" - "reflect" "regexp" "strconv" "strings" @@ -56,9 +55,7 @@ func init() { if env, exists := os.LookupEnv("CADDY_ADMIN"); exists { DefaultAdminListen = env } - RegisterType("caddy.config_loaders", []reflect.Type{ - reflect.TypeOf((*ConfigLoader)(nil)).Elem(), - }) + RegisterType("caddy.config_loaders", []interface{}{(*ConfigLoader)(nil)}) } // AdminConfig configures Caddy's API endpoint, which is used diff --git a/caddy.go b/caddy.go index d011b401..3b8f2851 100644 --- a/caddy.go +++ b/caddy.go @@ -27,7 +27,6 @@ import ( "os" "path" "path/filepath" - "reflect" "runtime/debug" "strconv" "strings" @@ -43,11 +42,11 @@ import ( ) func init() { - RegisterType("", []reflect.Type{ - reflect.TypeOf((*App)(nil)).Elem(), + RegisterType("", []interface{}{ + (*App)(nil), }) - RegisterType("caddy.storage", []reflect.Type{ - reflect.TypeOf((*StorageConverter)(nil)).Elem(), + RegisterType("caddy.storage", []interface{}{ + (*StorageConverter)(nil), }) } diff --git a/caddytest/integration/types_test.go b/caddytest/integration/types_test.go index 4ea28e98..8dd437b3 100644 --- a/caddytest/integration/types_test.go +++ b/caddytest/integration/types_test.go @@ -7,8 +7,8 @@ import ( _ "github.com/caddyserver/caddy/v2/modules/standard" ) -// Validates Caddy's the registered internal types implement the necessary interfaces of their -// namespaces +// Validates that Caddy's registered internal modules implement the necessary interfaces of their +// respective namespaces func TestTypes(t *testing.T) { var i int for _, v := range caddy.Modules() { diff --git a/listeners.go b/listeners.go index 7165e932..34e1fc16 100644 --- a/listeners.go +++ b/listeners.go @@ -24,7 +24,6 @@ import ( "net" "net/netip" "os" - "reflect" "strconv" "strings" "sync" @@ -39,8 +38,8 @@ import ( ) func init() { - RegisterType("caddy.listeners", []reflect.Type{ - reflect.TypeOf((*ListenerWrapper)(nil)).Elem(), + RegisterType("caddy.listeners", []interface{}{ + (*ListenerWrapper)(nil), }) } diff --git a/logging.go b/logging.go index 2ddb34be..22caa1fe 100644 --- a/logging.go +++ b/logging.go @@ -20,7 +20,6 @@ import ( "io" "log" "os" - "reflect" "strings" "sync" "time" @@ -34,11 +33,11 @@ func init() { RegisterModule(StdoutWriter{}) RegisterModule(StderrWriter{}) RegisterModule(DiscardWriter{}) - RegisterType("caddy.logging.encoders", []reflect.Type{ - reflect.TypeOf((*zapcore.Encoder)(nil)).Elem(), + RegisterType("caddy.logging.encoders", []interface{}{ + (*zapcore.Encoder)(nil), }) - RegisterType("caddy.logging.writers", []reflect.Type{ - reflect.TypeOf((*WriterOpener)(nil)).Elem(), + RegisterType("caddy.logging.writers", []interface{}{ + (*WriterOpener)(nil), }) } diff --git a/modules/caddyhttp/caddyauth/basicauth.go b/modules/caddyhttp/caddyauth/basicauth.go index 15381ac0..c3706563 100644 --- a/modules/caddyhttp/caddyauth/basicauth.go +++ b/modules/caddyhttp/caddyauth/basicauth.go @@ -21,7 +21,6 @@ import ( "fmt" weakrand "math/rand" "net/http" - "reflect" "strings" "sync" "time" @@ -34,11 +33,11 @@ import ( func init() { caddy.RegisterModule(HTTPBasicAuth{}) - caddy.RegisterType("http.authentication.hashes", []reflect.Type{ - reflect.TypeOf((*Comparer)(nil)).Elem(), + caddy.RegisterType("http.authentication.hashes", []interface{}{ + (*Comparer)(nil), }) - caddy.RegisterType("http.authentication.providers", []reflect.Type{ - reflect.TypeOf((*Authenticator)(nil)).Elem(), + caddy.RegisterType("http.authentication.providers", []interface{}{ + (*Authenticator)(nil), }) weakrand.Seed(time.Now().UnixNano()) diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go index da97ec9b..6787e60a 100644 --- a/modules/caddyhttp/encode/encode.go +++ b/modules/caddyhttp/encode/encode.go @@ -26,7 +26,6 @@ import ( "math" "net" "net/http" - "reflect" "sort" "strconv" "strings" @@ -38,8 +37,8 @@ import ( func init() { caddy.RegisterModule(Encode{}) - caddy.RegisterType("http.encoders", []reflect.Type{ - reflect.TypeOf((*Encoding)(nil)).Elem(), + caddy.RegisterType("http.encoders", []interface{}{ + (*Encoding)(nil), }) } diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index 548f062e..2413f224 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -26,7 +26,6 @@ import ( "os" "path" "path/filepath" - "reflect" "runtime" "strconv" "strings" @@ -42,8 +41,8 @@ import ( func init() { weakrand.Seed(time.Now().UnixNano()) - caddy.RegisterType("http.precompressed", []reflect.Type{ - reflect.TypeOf((*encode.Precompressed)(nil)).Elem(), + caddy.RegisterType("http.precompressed", []interface{}{ + (*encode.Precompressed)(nil), }) caddy.RegisterModule(FileServer{}) diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index c6d8eaa0..9a28f680 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -214,8 +214,8 @@ func init() { caddy.RegisterModule(MatchHeaderRE{}) caddy.RegisterModule(new(MatchProtocol)) caddy.RegisterModule(MatchNot{}) - caddy.RegisterType("http.matchers", []reflect.Type{ - reflect.TypeOf((*RequestMatcher)(nil)).Elem(), + caddy.RegisterType("http.matchers", []interface{}{ + (*RequestMatcher)(nil), }) } diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index b232247e..973f4713 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -27,7 +27,6 @@ import ( "net/netip" "net/textproto" "net/url" - "reflect" "strconv" "strings" "sync" @@ -47,17 +46,17 @@ import ( func init() { caddy.RegisterModule(Handler{}) - caddy.RegisterType("http.reverse_proxy.circuit_breakers", []reflect.Type{ - reflect.TypeOf((*CircuitBreaker)(nil)).Elem(), + caddy.RegisterType("http.reverse_proxy.circuit_breakers", []interface{}{ + (*CircuitBreaker)(nil), }) - caddy.RegisterType("http.reverse_proxy.selection_policies", []reflect.Type{ - reflect.TypeOf((*Selector)(nil)).Elem(), + caddy.RegisterType("http.reverse_proxy.selection_policies", []interface{}{ + (*Selector)(nil), }) - caddy.RegisterType("http.reverse_proxy.transport", []reflect.Type{ - reflect.TypeOf((*http.RoundTripper)(nil)).Elem(), + caddy.RegisterType("http.reverse_proxy.transport", []interface{}{ + (*http.RoundTripper)(nil), }) - caddy.RegisterType("http.reverse_proxy.upstreams", []reflect.Type{ - reflect.TypeOf((*UpstreamSource)(nil)).Elem(), + caddy.RegisterType("http.reverse_proxy.upstreams", []interface{}{ + (*UpstreamSource)(nil), }) } diff --git a/modules/caddyhttp/routes.go b/modules/caddyhttp/routes.go index fbbac9fb..e89c4700 100644 --- a/modules/caddyhttp/routes.go +++ b/modules/caddyhttp/routes.go @@ -18,14 +18,13 @@ import ( "encoding/json" "fmt" "net/http" - "reflect" "github.com/caddyserver/caddy/v2" ) func init() { - caddy.RegisterType("http.handlers", []reflect.Type{ - reflect.TypeOf((*MiddlewareHandler)(nil)).Elem(), + caddy.RegisterType("http.handlers", []interface{}{ + (*MiddlewareHandler)(nil), }) } diff --git a/modules/caddytls/automation.go b/modules/caddytls/automation.go index 1c331389..a537b6db 100644 --- a/modules/caddytls/automation.go +++ b/modules/caddytls/automation.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "net/http" - "reflect" "strings" "time" @@ -32,14 +31,14 @@ import ( ) func init() { - caddy.RegisterType("dns.provider", []reflect.Type{ - reflect.TypeOf((*acmez.Solver)(nil)).Elem(), + caddy.RegisterType("dns.provider", []interface{}{ + (*acmez.Solver)(nil), }) - caddy.RegisterType("tls.get_certificate", []reflect.Type{ - reflect.TypeOf((*certmagic.Manager)(nil)).Elem(), + caddy.RegisterType("tls.get_certificate", []interface{}{ + (*certmagic.Manager)(nil), }) - caddy.RegisterType("tls.issuance", []reflect.Type{ - reflect.TypeOf((*certmagic.Issuer)(nil)).Elem(), + caddy.RegisterType("tls.issuance", []interface{}{ + (*certmagic.Issuer)(nil), }) } diff --git a/modules/caddytls/connpolicy.go b/modules/caddytls/connpolicy.go index 811fbb03..1ab98a3e 100644 --- a/modules/caddytls/connpolicy.go +++ b/modules/caddytls/connpolicy.go @@ -23,7 +23,6 @@ import ( "io" "os" "path/filepath" - "reflect" "strings" "github.com/mholt/acmez" @@ -34,8 +33,8 @@ import ( func init() { caddy.RegisterModule(LeafCertClientAuth{}) - caddy.RegisterType("tls.handshake_match", []reflect.Type{ - reflect.TypeOf((*ConnectionMatcher)(nil)).Elem(), + caddy.RegisterType("tls.handshake_match", []interface{}{ + (*ConnectionMatcher)(nil), }) } diff --git a/modules/caddytls/sessiontickets.go b/modules/caddytls/sessiontickets.go index 312f072d..8456a80f 100644 --- a/modules/caddytls/sessiontickets.go +++ b/modules/caddytls/sessiontickets.go @@ -21,7 +21,6 @@ import ( "fmt" "io" "log" - "reflect" "runtime/debug" "sync" "time" @@ -30,8 +29,8 @@ import ( ) func init() { - caddy.RegisterType("tls.stek", []reflect.Type{ - reflect.TypeOf((*STEKProvider)(nil)).Elem(), + caddy.RegisterType("tls.stek", []interface{}{ + (*STEKProvider)(nil), }) } diff --git a/modules/caddytls/tls.go b/modules/caddytls/tls.go index 0db080b9..6606ef55 100644 --- a/modules/caddytls/tls.go +++ b/modules/caddytls/tls.go @@ -21,7 +21,6 @@ import ( "fmt" "log" "net/http" - "reflect" "runtime/debug" "sync" "time" @@ -37,8 +36,8 @@ func init() { caddy.RegisterModule(TLS{}) caddy.RegisterModule(AutomateLoader{}) - caddy.RegisterType("tls.certificates", []reflect.Type{ - reflect.TypeOf((*CertificateLoader)(nil)).Elem(), + caddy.RegisterType("tls.certificates", []interface{}{ + (*CertificateLoader)(nil), }) } diff --git a/types.go b/types.go index 22758e9d..6d4f6824 100644 --- a/types.go +++ b/types.go @@ -7,7 +7,11 @@ import ( var namespaceTypes map[string][]reflect.Type = make(map[string][]reflect.Type) -func RegisterType(namespace string, types []reflect.Type) { +func RegisterType(namespace string, vals []interface{}) { + var types []reflect.Type + for _, v := range vals { + reflect.TypeOf(v).Elem() + } if _, ok := namespaceTypes[namespace]; ok { panic("namespace is already registered") }