go-http-peering/server/option.go

57 lines
1.0 KiB
Go

package server
import (
"log"
"net/http"
"os"
)
type Logger interface {
Printf(string, ...interface{})
}
type Options struct {
PeerAttributes []string
ErrorHandler ErrorHandler
Logger Logger
}
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 defaultOptions() *Options {
logger := log.New(os.Stdout, "[go-http-peering] ", log.LstdFlags|log.Lshortfile)
return &Options{
PeerAttributes: []string{"Label"},
ErrorHandler: DefaultErrorHandler,
Logger: logger,
}
}
func createOptions(funcs ...OptionFunc) *Options {
options := defaultOptions()
for _, fn := range funcs {
fn(options)
}
return options
}