111 lines
2.1 KiB
Go
111 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"crypto/tls"
|
|
|
|
"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
|
|
TLSAddress string
|
|
TLSCertificate *tls.Certificate
|
|
EnableServiceDiscovery bool
|
|
EnableApps bool
|
|
DefaultApp string
|
|
AllowedOrigins []string
|
|
Apps []App
|
|
}
|
|
|
|
type OptionFunc func(opts *Options)
|
|
|
|
func NewOptions(funcs ...OptionFunc) *Options {
|
|
opts := &Options{
|
|
InstanceID: NewRandomInstanceID(),
|
|
Address: ":",
|
|
TLSAddress: ":",
|
|
EnableServiceDiscovery: true,
|
|
EnableApps: false,
|
|
DefaultApp: "",
|
|
AllowedOrigins: make([]string, 0),
|
|
Apps: make([]App, 0),
|
|
}
|
|
|
|
for _, fn := range funcs {
|
|
fn(opts)
|
|
}
|
|
|
|
return opts
|
|
}
|
|
|
|
func WithAppsEnabled(enabled bool) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.EnableApps = enabled
|
|
}
|
|
}
|
|
|
|
func WithDefaultApp(defaultApp string) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.DefaultApp = defaultApp
|
|
}
|
|
}
|
|
|
|
func WithApps(apps ...App) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.Apps = apps
|
|
}
|
|
}
|
|
|
|
func WithAllowedOrigins(origins ...string) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.AllowedOrigins = origins
|
|
}
|
|
}
|
|
|
|
func WithAddress(addr string) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.Address = addr
|
|
}
|
|
}
|
|
|
|
func WithTLSAddress(addr string) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.TLSAddress = addr
|
|
}
|
|
}
|
|
|
|
func WithTLSCertificate(cert *tls.Certificate) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.TLSCertificate = cert
|
|
}
|
|
}
|
|
|
|
func WithInstanceID(id string) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.InstanceID = id
|
|
}
|
|
}
|
|
|
|
func WithServiceDiscoveryEnabled(enabled bool) OptionFunc {
|
|
return func(opts *Options) {
|
|
opts.EnableServiceDiscovery = enabled
|
|
}
|
|
}
|
|
|
|
func NewRandomInstanceID() string {
|
|
return newRandomInstanceID()
|
|
}
|