go-http-peering/server/option.go

57 lines
1.0 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 {
2019-02-22 17:35:49 +01:00
PeerAttributes []string
ErrorHandler ErrorHandler
Logger Logger
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 defaultOptions() *Options {
logger := log.New(os.Stdout, "[go-http-peering] ", log.LstdFlags|log.Lshortfile)
return &Options{
2019-02-22 17:35:49 +01:00
PeerAttributes: []string{"Label"},
ErrorHandler: DefaultErrorHandler,
Logger: logger,
2019-02-03 20:56:58 +01:00
}
}
func createOptions(funcs ...OptionFunc) *Options {
options := defaultOptions()
for _, fn := range funcs {
fn(options)
}
return options
}