diff --git a/cmd/portal/init.go b/cmd/portal/init.go index 16e45ab..ba8d243 100644 --- a/cmd/portal/init.go +++ b/cmd/portal/init.go @@ -17,36 +17,6 @@ import ( "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) { var seed [32]byte identitySeed := config.Config().Core.Identity diff --git a/cmd/portal/main.go b/cmd/portal/main.go index a5e9dfb..1e8a2c2 100644 --- a/cmd/portal/main.go +++ b/cmd/portal/main.go @@ -54,7 +54,6 @@ func main() { fx.Supply(cfg), fx.Supply(logger, logLevel), fxLogger, - fx.Invoke(initCheckRequiredConfig), fx.Provide(NewIdentity), db.Module, renter.Module, diff --git a/config/core.go b/config/core.go index 4b1507a..f405f65 100644 --- a/config/core.go +++ b/config/core.go @@ -1,8 +1,13 @@ package config -import "github.com/docker/go-units" +import ( + "errors" + + "github.com/docker/go-units" +) var _ Defaults = (*CoreConfig)(nil) +var _ Validator = (*CoreConfig)(nil) type CoreConfig struct { DB DatabaseConfig `mapstructure:"db"` @@ -20,6 +25,20 @@ type CoreConfig struct { 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{} { return map[string]interface{}{ "post_upload_limit": units.MiB * 100, diff --git a/config/database.go b/config/database.go index dc3e106..8eca673 100644 --- a/config/database.go +++ b/config/database.go @@ -1,12 +1,14 @@ package config import ( + "errors" "reflect" "github.com/mitchellh/mapstructure" ) var _ Defaults = (*DatabaseConfig)(nil) +var _ Validator = (*DatabaseConfig)(nil) type DatabaseConfig struct { Charset string `mapstructure:"charset"` @@ -18,8 +20,29 @@ type DatabaseConfig struct { 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{} { return map[string]interface{}{ + "host": "localhost", "charset": "utf8mb4", "port": 3306, "name": "portal", diff --git a/config/s3.go b/config/s3.go index 8b5f312..3899fa8 100644 --- a/config/s3.go +++ b/config/s3.go @@ -1,5 +1,9 @@ package config +import "errors" + +var _ Validator = (*DatabaseConfig)(nil) + type S3Config struct { BufferBucket string `mapstructure:"buffer_bucket"` Endpoint string `mapstructure:"endpoint"` @@ -7,3 +11,22 @@ type S3Config struct { AccessKey string `mapstructure:"access_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 +} diff --git a/config/sia.go b/config/sia.go index 696978f..2d194db 100644 --- a/config/sia.go +++ b/config/sia.go @@ -1,6 +1,20 @@ package config +import "errors" + +var _ Validator = (*SiaConfig)(nil) + type SiaConfig struct { Key string `mapstructure:"key"` 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 +}