mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-26 13:43:47 +03:00
own the usage of reflection into the RegisterType
allowing the users to only pass instances of the interfaces
This commit is contained in:
parent
52f43d2f4c
commit
b825a10927
16 changed files with 50 additions and 61 deletions
5
admin.go
5
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
|
||||
|
|
9
caddy.go
9
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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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{})
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
6
types.go
6
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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue