159 lines
4.6 KiB
Go
159 lines
4.6 KiB
Go
package configs
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.vitatech.com.cn/leo/proto.git/discovery"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// Config 由各入口的 cmd/<name>/config 包定义。
|
|
type Config interface {
|
|
Validate() error
|
|
}
|
|
|
|
type RegistryConfig struct {
|
|
Etcd discovery.Config `mapstructure:"etcd"`
|
|
ServiceName string `mapstructure:"service_name"`
|
|
AdvertiseAddr string `mapstructure:"advertise_addr"`
|
|
LeaseTTL int64 `mapstructure:"lease_ttl"`
|
|
}
|
|
|
|
type AppConfig struct {
|
|
Name string `mapstructure:"name"`
|
|
Env string `mapstructure:"env"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Addr string `mapstructure:"addr"`
|
|
Reflection bool `mapstructure:"reflection"`
|
|
ShutdownTimeout time.Duration `mapstructure:"shutdown_timeout"`
|
|
}
|
|
|
|
type LogConfig struct {
|
|
Level string `mapstructure:"level"`
|
|
}
|
|
|
|
func Load(configFile string, cfg Config) error {
|
|
loader := viper.New()
|
|
loader.SetConfigType("yaml")
|
|
loader.AllowEmptyEnv(true)
|
|
loader.SetEnvPrefix("APP")
|
|
loader.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
loader.AutomaticEnv()
|
|
defaults := map[string]interface{}{
|
|
"app.name": "micro_layout",
|
|
"app.env": "local",
|
|
"server.addr": ":50112",
|
|
"server.reflection": false,
|
|
"server.shutdown_timeout": "10s",
|
|
"log.level": "info",
|
|
"registry.etcd.endpoints": []string{"127.0.0.1:2379"},
|
|
"registry.etcd.username": "",
|
|
"registry.etcd.password": "",
|
|
"registry.etcd.dial_timeout": "5s",
|
|
"registry.service_name": "micro.micro_layout",
|
|
"registry.advertise_addr": "",
|
|
"registry.lease_ttl": 10,
|
|
"storage.mysql.enabled": false,
|
|
"storage.mysql.dsn": "",
|
|
"storage.mysql.show_sql": false,
|
|
"storage.mysql.max_open_conn": 10,
|
|
"storage.mysql.max_idle_conn": 5,
|
|
"storage.mysql.conn_max_lifetime": "1h",
|
|
"storage.mysql.connect_timeout": "5s",
|
|
"storage.redis.enabled": false,
|
|
"storage.redis.addr": "127.0.0.1:6379",
|
|
"storage.redis.password": "",
|
|
"storage.redis.db": 0,
|
|
"storage.redis.connect_timeout": "5s",
|
|
"jetstream.enabled": false,
|
|
"jetstream.addr": "nats://127.0.0.1:4222",
|
|
"jetstream.token": "",
|
|
"jetstream.connect_timeout": "5s",
|
|
}
|
|
for key, value := range defaults {
|
|
loader.SetDefault(key, value)
|
|
}
|
|
if err := loader.BindEnv("app.name", "APP_NAME"); err != nil {
|
|
return err
|
|
}
|
|
if err := loader.BindEnv("app.env", "APP_ENV"); err != nil {
|
|
return err
|
|
}
|
|
if configFile == "" {
|
|
configFile = strings.TrimSpace(os.Getenv("CONFIG_FILE"))
|
|
}
|
|
if configFile != "" {
|
|
loader.SetConfigFile(configFile)
|
|
} else {
|
|
loader.SetConfigName("config")
|
|
loader.AddConfigPath("./configs")
|
|
}
|
|
if err := loader.ReadInConfig(); err != nil {
|
|
var notFound viper.ConfigFileNotFoundError
|
|
if configFile != "" || !errors.As(err, ¬Found) {
|
|
return errors.New("read configuration failed: check YAML syntax, file path and permissions")
|
|
}
|
|
}
|
|
if err := loader.UnmarshalExact(cfg); err != nil {
|
|
return errors.New("decode configuration failed: check field names and value types")
|
|
}
|
|
if err := cfg.Validate(); err != nil {
|
|
return fmt.Errorf("validate configuration: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cfg RegistryConfig) Validate() error {
|
|
if err := cfg.Etcd.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := discovery.ValidateService(cfg.ServiceName); err != nil {
|
|
return err
|
|
}
|
|
if cfg.LeaseTTL < 3 {
|
|
return errors.New("registry.lease_ttl must be at least 3")
|
|
}
|
|
if cfg.AdvertiseAddr != "" {
|
|
return discovery.ValidateAddress(cfg.AdvertiseAddr)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cfg AppConfig) Validate() error {
|
|
if strings.TrimSpace(cfg.Name) == "" || strings.TrimSpace(cfg.Env) == "" {
|
|
return errors.New("app.name and app.env must not be empty")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cfg ServerConfig) Validate() error {
|
|
_, port, err := net.SplitHostPort(cfg.Addr)
|
|
if err != nil {
|
|
return errors.New("server.addr must be host:port or :port")
|
|
}
|
|
number, err := strconv.Atoi(port)
|
|
if err != nil || number < 1 || number > 65535 {
|
|
return errors.New("server.addr port must be between 1 and 65535")
|
|
}
|
|
if cfg.ShutdownTimeout <= 0 {
|
|
return errors.New("server.shutdown_timeout must be greater than zero")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cfg LogConfig) Validate() error {
|
|
if _, err := log.ParseLevel(cfg.Level); err != nil {
|
|
return errors.New("log.level must be a valid logrus level")
|
|
}
|
|
return nil
|
|
}
|