refactor: move all config defaults and add some validations. remove initCheckRequiredConfig.

This commit is contained in:
Derrick Hammer 2024-02-28 09:04:47 -05:00
parent cb558cdfc3
commit 960c2b01d9
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
6 changed files with 80 additions and 32 deletions

View File

@ -17,36 +17,6 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
func initCheckRequiredConfig(logger *zap.Logger, config *config.Manager) error {
required := []string{
"core.domain",
"core.port",
"core.sia.url",
"core.sia.key",
"core.db.username",
"core.db.password",
"core.db.host",
"core.db.name",
"core.storage.s3.buffer_bucket",
"core.storage.s3.endpoint",
"core.storage.s3.region",
"core.storage.s3.access_key",
"core.storage.s3.secret_key",
"core.mail.host",
"core.mail.username",
"core.mail.password",
"core.portal_name",
}
for _, key := range required {
if !config.Viper().IsSet(key) {
logger.Fatal(key + " is required")
}
}
return nil
}
func NewIdentity(config *config.Manager, logger *zap.Logger) (ed25519.PrivateKey, error) { func NewIdentity(config *config.Manager, logger *zap.Logger) (ed25519.PrivateKey, error) {
var seed [32]byte var seed [32]byte
identitySeed := config.Config().Core.Identity identitySeed := config.Config().Core.Identity

View File

@ -54,7 +54,6 @@ func main() {
fx.Supply(cfg), fx.Supply(cfg),
fx.Supply(logger, logLevel), fx.Supply(logger, logLevel),
fxLogger, fxLogger,
fx.Invoke(initCheckRequiredConfig),
fx.Provide(NewIdentity), fx.Provide(NewIdentity),
db.Module, db.Module,
renter.Module, renter.Module,

View File

@ -1,8 +1,13 @@
package config package config
import "github.com/docker/go-units" import (
"errors"
"github.com/docker/go-units"
)
var _ Defaults = (*CoreConfig)(nil) var _ Defaults = (*CoreConfig)(nil)
var _ Validator = (*CoreConfig)(nil)
type CoreConfig struct { type CoreConfig struct {
DB DatabaseConfig `mapstructure:"db"` DB DatabaseConfig `mapstructure:"db"`
@ -20,6 +25,20 @@ type CoreConfig struct {
Clustered *ClusterConfig `mapstructure:"clustered"` Clustered *ClusterConfig `mapstructure:"clustered"`
} }
func (c CoreConfig) Validate() error {
if c.Domain == "" {
return errors.New("core.domain is required")
}
if c.PortalName == "" {
return errors.New("core.portal_name is required")
}
if c.Port == 0 {
return errors.New("core.port is required")
}
return nil
}
func (c CoreConfig) Defaults() map[string]interface{} { func (c CoreConfig) Defaults() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"post_upload_limit": units.MiB * 100, "post_upload_limit": units.MiB * 100,

View File

@ -1,12 +1,14 @@
package config package config
import ( import (
"errors"
"reflect" "reflect"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
) )
var _ Defaults = (*DatabaseConfig)(nil) var _ Defaults = (*DatabaseConfig)(nil)
var _ Validator = (*DatabaseConfig)(nil)
type DatabaseConfig struct { type DatabaseConfig struct {
Charset string `mapstructure:"charset"` Charset string `mapstructure:"charset"`
@ -18,8 +20,29 @@ type DatabaseConfig struct {
Cache *CacheConfig `mapstructure:"cache"` Cache *CacheConfig `mapstructure:"cache"`
} }
func (d DatabaseConfig) Validate() error {
if d.Host == "" {
return errors.New("core.db.host is required")
}
if d.Port == 0 {
return errors.New("core.db.port is required")
}
if d.Username == "" {
return errors.New("core.db.username is required")
}
if d.Password == "" {
return errors.New("core.db.password is required")
}
if d.Name == "" {
return errors.New("core.db.name is required")
}
return nil
}
func (d DatabaseConfig) Defaults() map[string]interface{} { func (d DatabaseConfig) Defaults() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"host": "localhost",
"charset": "utf8mb4", "charset": "utf8mb4",
"port": 3306, "port": 3306,
"name": "portal", "name": "portal",

View File

@ -1,5 +1,9 @@
package config package config
import "errors"
var _ Validator = (*DatabaseConfig)(nil)
type S3Config struct { type S3Config struct {
BufferBucket string `mapstructure:"buffer_bucket"` BufferBucket string `mapstructure:"buffer_bucket"`
Endpoint string `mapstructure:"endpoint"` Endpoint string `mapstructure:"endpoint"`
@ -7,3 +11,22 @@ type S3Config struct {
AccessKey string `mapstructure:"access_key"` AccessKey string `mapstructure:"access_key"`
SecretKey string `mapstructure:"secret_key"` SecretKey string `mapstructure:"secret_key"`
} }
func (s S3Config) Validate() error {
if s.BufferBucket == "" {
return errors.New("core.storage.s3.buffer_bucket is required")
}
if s.Endpoint == "" {
return errors.New("core.storage.s3.endpoint is required")
}
if s.Region == "" {
return errors.New("core.storage.s3.region is required")
}
if s.AccessKey == "" {
return errors.New("core.storage.s3.access_key is required")
}
if s.SecretKey == "" {
return errors.New("core.storage.s3.secret_key is required")
}
return nil
}

View File

@ -1,6 +1,20 @@
package config package config
import "errors"
var _ Validator = (*SiaConfig)(nil)
type SiaConfig struct { type SiaConfig struct {
Key string `mapstructure:"key"` Key string `mapstructure:"key"`
URL string `mapstructure:"url"` URL string `mapstructure:"url"`
} }
func (s SiaConfig) Validate() error {
if s.Key == "" {
return errors.New("core.storage.sia.key is required")
}
if s.URL == "" {
return errors.New("core.storage.sia.url is required")
}
return nil
}