arcast/pkg/server/options.go

62 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-12-13 20:07:22 +01:00
package server
import (
"github.com/jaevor/go-nanoid"
"github.com/pkg/errors"
)
var newRandomInstanceID func() string
func init() {
generator, err := nanoid.Standard(21)
if err != nil {
panic(errors.Wrap(err, "could not generate random instance id"))
}
newRandomInstanceID = generator
}
type Options struct {
InstanceID string
Address string
DisableServiceDiscovery bool
}
type OptionFunc func(opts *Options)
func NewOptions(funcs ...OptionFunc) *Options {
opts := &Options{
InstanceID: NewRandomInstanceID(),
Address: ":",
DisableServiceDiscovery: false,
}
for _, fn := range funcs {
fn(opts)
}
return opts
}
func WithAddress(addr string) OptionFunc {
return func(opts *Options) {
opts.Address = addr
}
}
func WithInstanceID(id string) OptionFunc {
return func(opts *Options) {
opts.InstanceID = id
}
}
func WithServiceDiscoveryDisabled(disabled bool) OptionFunc {
return func(opts *Options) {
opts.DisableServiceDiscovery = disabled
}
}
func NewRandomInstanceID() string {
return newRandomInstanceID()
}