2022-03-21 22:27:35 +00:00
|
|
|
package internal
|
|
|
|
|
2022-03-26 14:43:05 +00:00
|
|
|
import (
|
2022-03-31 10:36:47 +00:00
|
|
|
"fmt"
|
2022-03-26 14:43:05 +00:00
|
|
|
"net/url"
|
2022-03-31 10:36:47 +00:00
|
|
|
"os"
|
2022-03-26 14:43:05 +00:00
|
|
|
)
|
2022-03-22 22:59:09 +00:00
|
|
|
|
2022-03-21 22:27:35 +00:00
|
|
|
const (
|
2022-04-02 02:59:39 +00:00
|
|
|
serviceUser = "salty"
|
|
|
|
|
2022-03-21 22:27:35 +00:00
|
|
|
// InvalidConfigValue is the constant value for invalid config values
|
|
|
|
// which must be changed for production configurations before successful
|
|
|
|
// startup
|
|
|
|
InvalidConfigValue = "INVALID CONFIG VALUE - PLEASE CHANGE THIS VALUE"
|
|
|
|
|
|
|
|
// DefaultDebug is the default debug mode
|
|
|
|
DefaultDebug = false
|
|
|
|
|
2022-03-26 01:42:40 +00:00
|
|
|
// DefaultTLS is the default for whether to enable TLS
|
|
|
|
DefaultTLS = false
|
|
|
|
|
|
|
|
// DefaultTLSKey is the default path to a TLS private key (if blank uses Let's Encrypt)
|
|
|
|
DefaultTLSKey = ""
|
|
|
|
|
|
|
|
// DefaultTLSCert is the default path to a TLS certificate (if blank uses Let's Encrypt)
|
|
|
|
DefaultTLSCert = ""
|
|
|
|
|
2022-03-22 22:59:09 +00:00
|
|
|
// DefaultData is the default data directory for storage
|
|
|
|
DefaultData = "./data"
|
|
|
|
|
2022-03-21 22:27:35 +00:00
|
|
|
// DefaultStore is the default data store used for accounts, sessions, etc
|
2022-04-02 02:59:39 +00:00
|
|
|
DefaultStore = "bitcask://data/saltyim.db"
|
2022-03-22 22:59:09 +00:00
|
|
|
|
|
|
|
// DefaultBaseURL is the default Base URL for the server
|
2022-04-02 02:59:39 +00:00
|
|
|
DefaultBaseURL = "https://salty." + DefaultPrimaryDomain
|
2022-03-26 01:42:40 +00:00
|
|
|
|
2022-04-02 02:59:39 +00:00
|
|
|
// DefaultPrimaryDomain is the default primary domain delegated to this broker
|
|
|
|
DefaultPrimaryDomain = "home.arpa"
|
2022-03-26 14:43:05 +00:00
|
|
|
|
2022-03-26 01:42:40 +00:00
|
|
|
// DefaultAdminUser is the default publickye to grant admin privileges to
|
|
|
|
DefaultAdminUser = ""
|
|
|
|
|
2022-04-02 02:59:39 +00:00
|
|
|
// DefaultSupportEmail is the default email of the admin user used in support requests
|
|
|
|
DefaultSupportEmail = "support@" + DefaultPrimaryDomain
|
2022-03-21 22:27:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewConfig() *Config {
|
|
|
|
return &Config{
|
2022-04-02 02:59:39 +00:00
|
|
|
Debug: DefaultDebug,
|
|
|
|
Store: DefaultStore,
|
|
|
|
|
|
|
|
BaseURL: DefaultBaseURL,
|
|
|
|
PrimaryDomain: DefaultPrimaryDomain,
|
|
|
|
|
|
|
|
AdminUser: DefaultAdminUser,
|
|
|
|
SupportEmail: DefaultSupportEmail,
|
2022-03-21 22:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option is a function that takes a config struct and modifies it
|
|
|
|
type Option func(*Config) error
|
|
|
|
|
|
|
|
// WithDebug sets the debug mode lfag
|
|
|
|
func WithDebug(debug bool) Option {
|
|
|
|
return func(cfg *Config) error {
|
|
|
|
cfg.Debug = debug
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 01:42:40 +00:00
|
|
|
// WithTLS sets the tls flag
|
|
|
|
func WithTLS(tls bool) Option {
|
|
|
|
return func(cfg *Config) error {
|
|
|
|
cfg.TLS = tls
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTLSKey sets the path to a TLS private key
|
|
|
|
func WithTLSKey(tlsKey string) Option {
|
|
|
|
return func(cfg *Config) error {
|
|
|
|
cfg.TLSKey = tlsKey
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTLSCert sets the path to a TLS certificate
|
|
|
|
func WithTLSCert(tlsCert string) Option {
|
|
|
|
return func(cfg *Config) error {
|
|
|
|
cfg.TLSCert = tlsCert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 22:59:09 +00:00
|
|
|
// WithData sets the data directory to use for storage
|
|
|
|
func WithData(data string) Option {
|
|
|
|
return func(cfg *Config) error {
|
2022-03-31 10:36:47 +00:00
|
|
|
if err := os.MkdirAll(data, 0755); err != nil {
|
|
|
|
return fmt.Errorf("error creating data directory %s: %w", data, err)
|
|
|
|
}
|
2022-03-22 22:59:09 +00:00
|
|
|
cfg.Data = data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:27:35 +00:00
|
|
|
// WithStore sets the store to use for accounts, sessions, etc.
|
|
|
|
func WithStore(store string) Option {
|
|
|
|
return func(cfg *Config) error {
|
|
|
|
cfg.Store = store
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 22:59:09 +00:00
|
|
|
|
|
|
|
// WithBaseURL sets the Base URL used for constructing feed URLs
|
|
|
|
func WithBaseURL(baseURL string) Option {
|
|
|
|
return func(cfg *Config) error {
|
|
|
|
u, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cfg.BaseURL = baseURL
|
2022-10-15 21:45:13 +00:00
|
|
|
if baseURL != DefaultBaseURL {
|
|
|
|
cfg.BrokerURI = baseURL
|
|
|
|
}
|
|
|
|
|
2022-03-22 22:59:09 +00:00
|
|
|
cfg.baseURL = u
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 01:42:40 +00:00
|
|
|
|
2022-04-02 02:59:39 +00:00
|
|
|
// WithPrimaryDomain sets the Primary Domain this broker is delegated for
|
|
|
|
func WithPrimaryDomain(primaryDomain string) Option {
|
2022-03-26 01:42:40 +00:00
|
|
|
return func(cfg *Config) error {
|
2022-04-02 02:59:39 +00:00
|
|
|
cfg.PrimaryDomain = primaryDomain
|
2022-03-26 01:42:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 02:59:39 +00:00
|
|
|
// WithAdminUser sets the Admin user used for granting special features to
|
|
|
|
func WithAdminUser(adminUser string) Option {
|
2022-03-26 01:42:40 +00:00
|
|
|
return func(cfg *Config) error {
|
2022-04-02 02:59:39 +00:00
|
|
|
cfg.AdminUser = adminUser
|
2022-03-26 01:42:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 14:43:05 +00:00
|
|
|
|
2022-04-02 02:59:39 +00:00
|
|
|
// WithSupportEmail sets the Support email used to contact the operator of this broker
|
|
|
|
func WithSupportEmail(supportEmail string) Option {
|
|
|
|
return func(cfg *Config) error {
|
|
|
|
cfg.SupportEmail = supportEmail
|
2022-03-26 14:43:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|