own the usage of reflection into the RegisterType

allowing the users to only pass instances of the interfaces
This commit is contained in:
Mohammed Al Sahaf
2022-06-14 20:51:58 +03:00
parent 52f43d2f4c
commit b825a10927
16 changed files with 50 additions and 61 deletions
+4 -5
View File
@@ -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())
+2 -3
View File
@@ -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),
})
}
+2 -3
View File
@@ -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{})
+2 -2
View File
@@ -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),
})
}
@@ -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),
})
}
+2 -3
View File
@@ -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),
})
}