65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
type Logger interface {
|
|
Printf(string, ...interface{})
|
|
}
|
|
|
|
type Options struct {
|
|
PeerAttributes []string
|
|
ErrorHandler ErrorHandler
|
|
Logger Logger
|
|
IgnoredClientTokenErrors []uint32
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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),
|
|
}
|
|
}
|
|
|
|
func createOptions(funcs ...OptionFunc) *Options {
|
|
options := defaultOptions()
|
|
for _, fn := range funcs {
|
|
fn(options)
|
|
}
|
|
return options
|
|
}
|