add options for components

This commit is contained in:
ginuerzh 2023-10-26 22:20:46 +08:00
parent 6431cd8bb9
commit 9e767d6745
6 changed files with 37 additions and 11 deletions

View File

@ -2,8 +2,11 @@ package admission
import "context"
type Options struct{}
type Option func(opts *Options)
type Admission interface {
Admit(ctx context.Context, addr string) bool
Admit(ctx context.Context, addr string, opts ...Option) bool
}
type admissionGroup struct {
@ -16,9 +19,9 @@ func AdmissionGroup(admissions ...Admission) Admission {
}
}
func (p *admissionGroup) Admit(ctx context.Context, addr string) bool {
func (p *admissionGroup) Admit(ctx context.Context, addr string, opts ...Option) bool {
for _, admission := range p.admissions {
if admission != nil && !admission.Admit(ctx, addr) {
if admission != nil && !admission.Admit(ctx, addr, opts...) {
return false
}
}

View File

@ -2,9 +2,13 @@ package auth
import "context"
type Options struct{}
type Option func(opts *Options)
// Authenticator is an interface for user authentication.
type Authenticator interface {
Authenticate(ctx context.Context, user, password string) (id string, ok bool)
Authenticate(ctx context.Context, user, password string, opts ...Option) (id string, ok bool)
}
type authenticatorGroup struct {
@ -17,7 +21,7 @@ func AuthenticatorGroup(authers ...Authenticator) Authenticator {
}
}
func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password string) (string, bool) {
func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password string, opts ...Option) (string, bool) {
if len(p.authers) == 0 {
return "", false
}
@ -26,7 +30,7 @@ func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password st
continue
}
if id, ok := auther.Authenticate(ctx, user, password); ok {
if id, ok := auther.Authenticate(ctx, user, password, opts...); ok {
return id, ok
}
}

View File

@ -5,7 +5,11 @@ import (
"net"
)
type Options struct{}
type Option func(opts *Options)
// HostMapper is a mapping from hostname to IP.
type HostMapper interface {
Lookup(ctx context.Context, network, host string) ([]net.IP, bool)
Lookup(ctx context.Context, network, host string, opts ...Option) ([]net.IP, bool)
}

View File

@ -2,7 +2,15 @@ package ingress
import "context"
type GetOptions struct{}
type GetOption func(opts *GetOptions)
type SetOptions struct{}
type SetOption func(opts *SetOptions)
type Ingress interface {
Get(ctx context.Context, host string) string
Set(ctx context.Context, host, endpoint string)
Get(ctx context.Context, host string, opts ...GetOption) string
Set(ctx context.Context, host, endpoint string, opts ...SetOption)
}

View File

@ -4,8 +4,11 @@ import (
"context"
)
type RecordOptions struct{}
type RecordOption func(opts *RecordOptions)
type Recorder interface {
Record(ctx context.Context, b []byte) error
Record(ctx context.Context, b []byte, opts ...RecordOption) error
}
type RecorderObject struct {

View File

@ -10,8 +10,12 @@ var (
ErrInvalid = errors.New("invalid resolver")
)
type Options struct{}
type Option func(opts *Options)
type Resolver interface {
// Resolve returns a slice of the host's IPv4 and IPv6 addresses.
// The network should be 'ip', 'ip4' or 'ip6', default network is 'ip'.
Resolve(ctx context.Context, network, host string) ([]net.IP, error)
Resolve(ctx context.Context, network, host string, opts ...Option) ([]net.IP, error)
}