go-http-peering/server/option.go

65 lines
1.3 KiB
Go
Raw Normal View History

2019-02-03 20:56:58 +01:00
package server
import (
"log"
"net/http"
"os"
)
type Logger interface {
Printf(string, ...interface{})
}
type Options struct {
PeerAttributes []string
ErrorHandler ErrorHandler
Logger Logger
IgnoredClientTokenErrors []uint32
2019-02-03 20:56:58 +01:00
}
type OptionFunc func(*Options)
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
func WithPeerAttributes(attrs ...string) OptionFunc {
return func(options *Options) {
options.PeerAttributes = attrs
}
}
func WithLogger(logger Logger) OptionFunc {
return func(options *Options) {
options.Logger = logger
}
}
func WithErrorHandler(handler ErrorHandler) OptionFunc {
return func(options *Options) {
options.ErrorHandler = handler
}
}
func WithIgnoredClientTokenErrors(codes ...uint32) OptionFunc {
return func(options *Options) {
options.IgnoredClientTokenErrors = codes
}
}
2019-02-03 20:56:58 +01:00
func defaultOptions() *Options {
logger := log.New(os.Stdout, "[go-http-peering] ", log.LstdFlags|log.Lshortfile)
return &Options{
PeerAttributes: []string{"Label"},
ErrorHandler: DefaultErrorHandler,
Logger: logger,
IgnoredClientTokenErrors: make([]uint32, 0),
2019-02-03 20:56:58 +01:00
}
}
func createOptions(funcs ...OptionFunc) *Options {
options := defaultOptions()
for _, fn := range funcs {
fn(options)
}
return options
}