|
@@ -5,19 +5,24 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"os"
|
|
"os"
|
|
|
|
|
|
|
|
+ "git.linuxforward.com/byom/byom-golang-lib/pkg/auth"
|
|
|
|
+ "git.linuxforward.com/byom/byom-golang-lib/pkg/database"
|
|
"git.linuxforward.com/byom/byom-golang-lib/pkg/errors"
|
|
"git.linuxforward.com/byom/byom-golang-lib/pkg/errors"
|
|
- "github.com/sirupsen/logrus"
|
|
|
|
|
|
+ "git.linuxforward.com/byom/byom-golang-lib/pkg/logger"
|
|
|
|
+ "git.linuxforward.com/byom/byom-golang-lib/pkg/server"
|
|
|
|
+ "git.linuxforward.com/byom/byom-golang-lib/pkg/storage"
|
|
"gopkg.in/yaml.v3"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
)
|
|
|
|
|
|
type Config struct {
|
|
type Config struct {
|
|
- App *App `yaml:"app"`
|
|
|
|
- Server *Server `yaml:"server"`
|
|
|
|
- Database *Database `yaml:"database"`
|
|
|
|
- Log *Log `yaml:"log"`
|
|
|
|
- Storage *Storage `yaml:"storage"`
|
|
|
|
- CloudComputing *CloudComputing `yaml:"cloud_computing,omitempty"`
|
|
|
|
- SocialNetworks *SocialNetworks `yaml:"social_networks,omitempty"`
|
|
|
|
|
|
+ App *App `yaml:"app"`
|
|
|
|
+ Auth *auth.Config `yaml:"auth"`
|
|
|
|
+ Server *server.Config `yaml:"server"`
|
|
|
|
+ Database *database.Config `yaml:"database"`
|
|
|
|
+ Log *logger.Config `yaml:"log"`
|
|
|
|
+ Storage *storage.Config `yaml:"storage"`
|
|
|
|
+ CloudComputing *CloudComputing `yaml:"cloud_computing,omitempty"`
|
|
|
|
+ SocialNetworks *SocialNetworks `yaml:"social_networks,omitempty"`
|
|
}
|
|
}
|
|
|
|
|
|
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
@@ -69,111 +74,6 @@ func (c *App) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-type Server struct {
|
|
|
|
- Port int `yaml:"port"`
|
|
|
|
- RequestTimeout int `yaml:"request_timeout"` // in seconds
|
|
|
|
- AllowedOrigins []string `yaml:"allowed_origins"`
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (c *Server) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
- type plain Server
|
|
|
|
- err := unmarshal((*plain)(c))
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- if c.Port == 0 {
|
|
|
|
- c.Port = 8080
|
|
|
|
- }
|
|
|
|
- if c.RequestTimeout == 0 {
|
|
|
|
- c.RequestTimeout = 30
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-type Database struct {
|
|
|
|
- Path string `yaml:"path"`
|
|
|
|
- MaxOpenConns int `yaml:"max_open_conns"`
|
|
|
|
- MaxIdleConns int `yaml:"max_idle_conns"`
|
|
|
|
- ConnMaxLifetime int `yaml:"conn_max_lifetime"`
|
|
|
|
- Pragmas []string `yaml:"pragmas"`
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (c *Database) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
- type plain Database
|
|
|
|
- err := unmarshal((*plain)(c))
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- if c.Path == "" {
|
|
|
|
- return errors.NewConfigError("database.path", errors.ErrInvalidInput)
|
|
|
|
- }
|
|
|
|
- if c.MaxOpenConns == 0 {
|
|
|
|
- c.MaxOpenConns = 1 // SQLite recommendation
|
|
|
|
- }
|
|
|
|
- if c.MaxIdleConns == 0 {
|
|
|
|
- c.MaxIdleConns = 1
|
|
|
|
- }
|
|
|
|
- if c.ConnMaxLifetime == 0 {
|
|
|
|
- c.ConnMaxLifetime = 300 // 5 minutes
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-type Log struct {
|
|
|
|
- Level string `yaml:"level"`
|
|
|
|
- Format string `yaml:"format"` // "json" or "text"
|
|
|
|
- NoColor bool `yaml:"no_color"`
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (c *Log) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
- type plain Log
|
|
|
|
- err := unmarshal((*plain)(c))
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- if c.Level == "" {
|
|
|
|
- c.Level = "info"
|
|
|
|
- }
|
|
|
|
- if c.Format == "" {
|
|
|
|
- c.Format = "text"
|
|
|
|
- }
|
|
|
|
- // Validate log level
|
|
|
|
- _, err = logrus.ParseLevel(c.Level)
|
|
|
|
- if err != nil {
|
|
|
|
- return errors.NewConfigError("log.level", err)
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-type Storage struct {
|
|
|
|
- Endpoint string `yaml:"endpoint"`
|
|
|
|
- AccessKeyID string `yaml:"access_key_id"`
|
|
|
|
- SecretAccessKey string `yaml:"secret_access_key"`
|
|
|
|
- UseSSL bool `yaml:"use_ssl"`
|
|
|
|
- BucketName string `yaml:"bucket_name"`
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (c *Storage) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
- type plain Storage
|
|
|
|
- err := unmarshal((*plain)(c))
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
- if c.Endpoint == "" {
|
|
|
|
- return errors.NewConfigError("storage.endpoint", errors.ErrInvalidInput)
|
|
|
|
- }
|
|
|
|
- if c.AccessKeyID == "" {
|
|
|
|
- return errors.NewConfigError("storage.access_key_id", errors.ErrInvalidInput)
|
|
|
|
- }
|
|
|
|
- if c.SecretAccessKey == "" {
|
|
|
|
- return errors.NewConfigError("storage.secret_access_key", errors.ErrInvalidInput)
|
|
|
|
- }
|
|
|
|
- if c.BucketName == "" {
|
|
|
|
- return errors.NewConfigError("storage.bucket_name", errors.ErrInvalidInput)
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
type CloudComputing struct {
|
|
type CloudComputing struct {
|
|
Provider string `yaml:"provider"`
|
|
Provider string `yaml:"provider"`
|
|
Region string `yaml:"region"`
|
|
Region string `yaml:"region"`
|
|
@@ -244,5 +144,28 @@ func ReadConfig(configPath string) (*Config, error) {
|
|
return nil, errors.NewConfigError("parse", err)
|
|
return nil, errors.NewConfigError("parse", err)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Validate all config sections that implement Validator interface
|
|
|
|
+ validators := []Validator{}
|
|
|
|
+
|
|
|
|
+ if config.Storage != nil {
|
|
|
|
+ validators = append(validators, config.Storage)
|
|
|
|
+ }
|
|
|
|
+ if config.Database != nil {
|
|
|
|
+ validators = append(validators, config.Database)
|
|
|
|
+ }
|
|
|
|
+ if config.Log != nil {
|
|
|
|
+ validators = append(validators, config.Log)
|
|
|
|
+ }
|
|
|
|
+ if config.Server != nil {
|
|
|
|
+ validators = append(validators, config.Server)
|
|
|
|
+ }
|
|
|
|
+ if config.Auth != nil {
|
|
|
|
+ validators = append(validators, config.Auth)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err := ValidateAll(validators...); err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
return config, nil
|
|
return config, nil
|
|
}
|
|
}
|