arcast/pkg/server/options.go

72 lines
1.3 KiB
Go

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
WebApps bool
DefaultWebApp string
}
type OptionFunc func(opts *Options)
func NewOptions(funcs ...OptionFunc) *Options {
opts := &Options{
InstanceID: NewRandomInstanceID(),
Address: ":",
DisableServiceDiscovery: false,
WebApps: false,
DefaultWebApp: "home",
}
for _, fn := range funcs {
fn(opts)
}
return opts
}
func WithWebApps(enabled bool) OptionFunc {
return func(opts *Options) {
opts.WebApps = enabled
}
}
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()
}