mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-22 16:46:53 +01: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"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"reflect"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -56,9 +55,7 @@ func init() {
|
||||||
if env, exists := os.LookupEnv("CADDY_ADMIN"); exists {
|
if env, exists := os.LookupEnv("CADDY_ADMIN"); exists {
|
||||||
DefaultAdminListen = env
|
DefaultAdminListen = env
|
||||||
}
|
}
|
||||||
RegisterType("caddy.config_loaders", []reflect.Type{
|
RegisterType("caddy.config_loaders", []interface{}{(*ConfigLoader)(nil)})
|
||||||
reflect.TypeOf((*ConfigLoader)(nil)).Elem(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdminConfig configures Caddy's API endpoint, which is used
|
// AdminConfig configures Caddy's API endpoint, which is used
|
||||||
|
|
9
caddy.go
9
caddy.go
|
@ -27,7 +27,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -43,11 +42,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterType("", []reflect.Type{
|
RegisterType("", []interface{}{
|
||||||
reflect.TypeOf((*App)(nil)).Elem(),
|
(*App)(nil),
|
||||||
})
|
})
|
||||||
RegisterType("caddy.storage", []reflect.Type{
|
RegisterType("caddy.storage", []interface{}{
|
||||||
reflect.TypeOf((*StorageConverter)(nil)).Elem(),
|
(*StorageConverter)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,8 @@ import (
|
||||||
_ "github.com/caddyserver/caddy/v2/modules/standard"
|
_ "github.com/caddyserver/caddy/v2/modules/standard"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Validates Caddy's the registered internal types implement the necessary interfaces of their
|
// Validates that Caddy's registered internal modules implement the necessary interfaces of their
|
||||||
// namespaces
|
// respective namespaces
|
||||||
func TestTypes(t *testing.T) {
|
func TestTypes(t *testing.T) {
|
||||||
var i int
|
var i int
|
||||||
for _, v := range caddy.Modules() {
|
for _, v := range caddy.Modules() {
|
||||||
|
|
|
@ -24,7 +24,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -39,8 +38,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterType("caddy.listeners", []reflect.Type{
|
RegisterType("caddy.listeners", []interface{}{
|
||||||
reflect.TypeOf((*ListenerWrapper)(nil)).Elem(),
|
(*ListenerWrapper)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -34,11 +33,11 @@ func init() {
|
||||||
RegisterModule(StdoutWriter{})
|
RegisterModule(StdoutWriter{})
|
||||||
RegisterModule(StderrWriter{})
|
RegisterModule(StderrWriter{})
|
||||||
RegisterModule(DiscardWriter{})
|
RegisterModule(DiscardWriter{})
|
||||||
RegisterType("caddy.logging.encoders", []reflect.Type{
|
RegisterType("caddy.logging.encoders", []interface{}{
|
||||||
reflect.TypeOf((*zapcore.Encoder)(nil)).Elem(),
|
(*zapcore.Encoder)(nil),
|
||||||
})
|
})
|
||||||
RegisterType("caddy.logging.writers", []reflect.Type{
|
RegisterType("caddy.logging.writers", []interface{}{
|
||||||
reflect.TypeOf((*WriterOpener)(nil)).Elem(),
|
(*WriterOpener)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
weakrand "math/rand"
|
weakrand "math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -34,11 +33,11 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterModule(HTTPBasicAuth{})
|
caddy.RegisterModule(HTTPBasicAuth{})
|
||||||
|
|
||||||
caddy.RegisterType("http.authentication.hashes", []reflect.Type{
|
caddy.RegisterType("http.authentication.hashes", []interface{}{
|
||||||
reflect.TypeOf((*Comparer)(nil)).Elem(),
|
(*Comparer)(nil),
|
||||||
})
|
})
|
||||||
caddy.RegisterType("http.authentication.providers", []reflect.Type{
|
caddy.RegisterType("http.authentication.providers", []interface{}{
|
||||||
reflect.TypeOf((*Authenticator)(nil)).Elem(),
|
(*Authenticator)(nil),
|
||||||
})
|
})
|
||||||
|
|
||||||
weakrand.Seed(time.Now().UnixNano())
|
weakrand.Seed(time.Now().UnixNano())
|
||||||
|
|
|
@ -26,7 +26,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -38,8 +37,8 @@ import (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterModule(Encode{})
|
caddy.RegisterModule(Encode{})
|
||||||
caddy.RegisterType("http.encoders", []reflect.Type{
|
caddy.RegisterType("http.encoders", []interface{}{
|
||||||
reflect.TypeOf((*Encoding)(nil)).Elem(),
|
(*Encoding)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -42,8 +41,8 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
weakrand.Seed(time.Now().UnixNano())
|
weakrand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
caddy.RegisterType("http.precompressed", []reflect.Type{
|
caddy.RegisterType("http.precompressed", []interface{}{
|
||||||
reflect.TypeOf((*encode.Precompressed)(nil)).Elem(),
|
(*encode.Precompressed)(nil),
|
||||||
})
|
})
|
||||||
|
|
||||||
caddy.RegisterModule(FileServer{})
|
caddy.RegisterModule(FileServer{})
|
||||||
|
|
|
@ -214,8 +214,8 @@ func init() {
|
||||||
caddy.RegisterModule(MatchHeaderRE{})
|
caddy.RegisterModule(MatchHeaderRE{})
|
||||||
caddy.RegisterModule(new(MatchProtocol))
|
caddy.RegisterModule(new(MatchProtocol))
|
||||||
caddy.RegisterModule(MatchNot{})
|
caddy.RegisterModule(MatchNot{})
|
||||||
caddy.RegisterType("http.matchers", []reflect.Type{
|
caddy.RegisterType("http.matchers", []interface{}{
|
||||||
reflect.TypeOf((*RequestMatcher)(nil)).Elem(),
|
(*RequestMatcher)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,6 @@ import (
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -47,17 +46,17 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterModule(Handler{})
|
caddy.RegisterModule(Handler{})
|
||||||
|
|
||||||
caddy.RegisterType("http.reverse_proxy.circuit_breakers", []reflect.Type{
|
caddy.RegisterType("http.reverse_proxy.circuit_breakers", []interface{}{
|
||||||
reflect.TypeOf((*CircuitBreaker)(nil)).Elem(),
|
(*CircuitBreaker)(nil),
|
||||||
})
|
})
|
||||||
caddy.RegisterType("http.reverse_proxy.selection_policies", []reflect.Type{
|
caddy.RegisterType("http.reverse_proxy.selection_policies", []interface{}{
|
||||||
reflect.TypeOf((*Selector)(nil)).Elem(),
|
(*Selector)(nil),
|
||||||
})
|
})
|
||||||
caddy.RegisterType("http.reverse_proxy.transport", []reflect.Type{
|
caddy.RegisterType("http.reverse_proxy.transport", []interface{}{
|
||||||
reflect.TypeOf((*http.RoundTripper)(nil)).Elem(),
|
(*http.RoundTripper)(nil),
|
||||||
})
|
})
|
||||||
caddy.RegisterType("http.reverse_proxy.upstreams", []reflect.Type{
|
caddy.RegisterType("http.reverse_proxy.upstreams", []interface{}{
|
||||||
reflect.TypeOf((*UpstreamSource)(nil)).Elem(),
|
(*UpstreamSource)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,14 +18,13 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterType("http.handlers", []reflect.Type{
|
caddy.RegisterType("http.handlers", []interface{}{
|
||||||
reflect.TypeOf((*MiddlewareHandler)(nil)).Elem(),
|
(*MiddlewareHandler)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -32,14 +31,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterType("dns.provider", []reflect.Type{
|
caddy.RegisterType("dns.provider", []interface{}{
|
||||||
reflect.TypeOf((*acmez.Solver)(nil)).Elem(),
|
(*acmez.Solver)(nil),
|
||||||
})
|
})
|
||||||
caddy.RegisterType("tls.get_certificate", []reflect.Type{
|
caddy.RegisterType("tls.get_certificate", []interface{}{
|
||||||
reflect.TypeOf((*certmagic.Manager)(nil)).Elem(),
|
(*certmagic.Manager)(nil),
|
||||||
})
|
})
|
||||||
caddy.RegisterType("tls.issuance", []reflect.Type{
|
caddy.RegisterType("tls.issuance", []interface{}{
|
||||||
reflect.TypeOf((*certmagic.Issuer)(nil)).Elem(),
|
(*certmagic.Issuer)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mholt/acmez"
|
"github.com/mholt/acmez"
|
||||||
|
@ -34,8 +33,8 @@ import (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterModule(LeafCertClientAuth{})
|
caddy.RegisterModule(LeafCertClientAuth{})
|
||||||
caddy.RegisterType("tls.handshake_match", []reflect.Type{
|
caddy.RegisterType("tls.handshake_match", []interface{}{
|
||||||
reflect.TypeOf((*ConnectionMatcher)(nil)).Elem(),
|
(*ConnectionMatcher)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -30,8 +29,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterType("tls.stek", []reflect.Type{
|
caddy.RegisterType("tls.stek", []interface{}{
|
||||||
reflect.TypeOf((*STEKProvider)(nil)).Elem(),
|
(*STEKProvider)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -37,8 +36,8 @@ func init() {
|
||||||
caddy.RegisterModule(TLS{})
|
caddy.RegisterModule(TLS{})
|
||||||
caddy.RegisterModule(AutomateLoader{})
|
caddy.RegisterModule(AutomateLoader{})
|
||||||
|
|
||||||
caddy.RegisterType("tls.certificates", []reflect.Type{
|
caddy.RegisterType("tls.certificates", []interface{}{
|
||||||
reflect.TypeOf((*CertificateLoader)(nil)).Elem(),
|
(*CertificateLoader)(nil),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
6
types.go
6
types.go
|
@ -7,7 +7,11 @@ import (
|
||||||
|
|
||||||
var namespaceTypes map[string][]reflect.Type = make(map[string][]reflect.Type)
|
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 {
|
if _, ok := namespaceTypes[namespace]; ok {
|
||||||
panic("namespace is already registered")
|
panic("namespace is already registered")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue