2023-12-13 20:07:22 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-01-16 09:27:04 +01:00
|
|
|
"crypto/tls"
|
|
|
|
|
2023-12-13 20:07:22 +01:00
|
|
|
"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 {
|
2024-01-16 09:27:04 +01:00
|
|
|
InstanceID string
|
|
|
|
Address string
|
|
|
|
TLSAddress string
|
|
|
|
TLSCertificate *tls.Certificate
|
|
|
|
EnableServiceDiscovery bool
|
|
|
|
EnableApps bool
|
|
|
|
DefaultApp string
|
2024-04-24 17:32:01 +02:00
|
|
|
AllowedOrigins []string
|
2024-01-16 09:27:04 +01:00
|
|
|
Apps []App
|
2024-04-26 12:10:59 +02:00
|
|
|
UpperLayerDir string
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type OptionFunc func(opts *Options)
|
|
|
|
|
|
|
|
func NewOptions(funcs ...OptionFunc) *Options {
|
|
|
|
opts := &Options{
|
2024-01-16 09:27:04 +01:00
|
|
|
InstanceID: NewRandomInstanceID(),
|
|
|
|
Address: ":",
|
|
|
|
TLSAddress: ":",
|
|
|
|
EnableServiceDiscovery: true,
|
|
|
|
EnableApps: false,
|
|
|
|
DefaultApp: "",
|
2024-04-24 17:32:01 +02:00
|
|
|
AllowedOrigins: make([]string, 0),
|
2024-01-16 09:27:04 +01:00
|
|
|
Apps: make([]App, 0),
|
2024-04-26 12:10:59 +02:00
|
|
|
UpperLayerDir: "",
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range funcs {
|
|
|
|
fn(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2024-01-16 09:27:04 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 17:32:01 +02:00
|
|
|
func WithAllowedOrigins(origins ...string) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.AllowedOrigins = origins
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 20:07:22 +01:00
|
|
|
func WithAddress(addr string) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Address = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 09:27:04 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 20:07:22 +01:00
|
|
|
func WithInstanceID(id string) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.InstanceID = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 09:27:04 +01:00
|
|
|
func WithServiceDiscoveryEnabled(enabled bool) OptionFunc {
|
2023-12-13 20:07:22 +01:00
|
|
|
return func(opts *Options) {
|
2024-01-16 09:27:04 +01:00
|
|
|
opts.EnableServiceDiscovery = enabled
|
2023-12-13 20:07:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 12:10:59 +02:00
|
|
|
func WithUpperLayerDir(dir string) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.UpperLayerDir = dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 20:07:22 +01:00
|
|
|
func NewRandomInstanceID() string {
|
|
|
|
return newRandomInstanceID()
|
|
|
|
}
|