61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
peering "forge.cadoles.com/wpetit/go-http-peering"
|
|
)
|
|
|
|
type Logger interface {
|
|
Printf(string, ...interface{})
|
|
}
|
|
|
|
type Options struct {
|
|
PeerAttributes []string
|
|
ErrorHandler ErrorHandler
|
|
PeerIDValidator func(peering.PeerID) bool
|
|
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,
|
|
PeerIDValidator: DefaultPeerIDValidator,
|
|
Logger: logger,
|
|
}
|
|
}
|
|
|
|
func createOptions(funcs ...OptionFunc) *Options {
|
|
options := defaultOptions()
|
|
for _, fn := range funcs {
|
|
fn(options)
|
|
}
|
|
return options
|
|
}
|