github.com/kuoss/venti@v0.2.20/pkg/config/config.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "os" 6 "time" 7 8 "github.com/gin-gonic/gin" 9 "github.com/kuoss/common/logger" 10 "github.com/kuoss/venti/pkg/model" 11 "gopkg.in/yaml.v2" 12 ) 13 14 // Load EtcUser, DatasourceConfig files only. 15 // TODO: each Config filepath could be parameter. 16 func Load(version string) (*model.Config, error) { 17 logger.Infof("loading configurations...") 18 19 appInfo := model.AppInfo{ 20 Version: version, 21 } 22 23 globalConfig, err := loadGlobalConfigFile("etc/venti.yml") 24 if err != nil { 25 return nil, fmt.Errorf("loadGlobalConfigFile err: %w", err) 26 } 27 28 datasourceConfig, err := loadDatasourceConfigFile("etc/datasources.yml") 29 if err != nil { 30 return nil, fmt.Errorf("loadDatasourceConfigFile err: %w", err) 31 } 32 33 userConfig, err := loadUserConfigFile("etc/users.yml") 34 if err != nil { 35 return nil, fmt.Errorf("loadUserConfigFile err: %w", err) 36 } 37 38 alertingConfig, err := loadAlertingConfigFile("etc/alerting.yml") 39 if err != nil { 40 return nil, fmt.Errorf("loadAlertingConfigFile err: %w", err) 41 } 42 43 return &model.Config{ 44 AppInfo: appInfo, 45 GlobalConfig: globalConfig, 46 DatasourceConfig: *datasourceConfig, 47 UserConfig: *userConfig, 48 AlertingConfig: alertingConfig, 49 }, nil 50 } 51 52 func loadGlobalConfigFile(file string) (model.GlobalConfig, error) { 53 var globalConfig model.GlobalConfig 54 logger.Infof("loading global config file: %s", file) 55 yamlBytes, err := os.ReadFile(file) 56 if err != nil { 57 return globalConfig, fmt.Errorf("error on ReadFile: %w", err) 58 } 59 if err := yaml.UnmarshalStrict(yamlBytes, &globalConfig); err != nil { 60 return globalConfig, fmt.Errorf("error on UnmarshalStrict: %w", err) 61 } 62 63 // ginMode 64 ginMode := globalConfig.GinMode 65 switch ginMode { 66 case gin.DebugMode: 67 case gin.ReleaseMode: 68 case gin.TestMode: 69 default: 70 logger.Warnf("gin mode unknown: %s (available mode: debug release test)", globalConfig.GinMode) 71 ginMode = gin.ReleaseMode 72 } 73 gin.SetMode(ginMode) 74 75 // logLevel 76 logLevel, err := logger.ParseLevel(globalConfig.LogLevel) 77 if err != nil { 78 logger.Warnf("ParseLevel err: %s", err) 79 logLevel = logger.InfoLevel 80 } 81 logger.SetLevel(logLevel) 82 return globalConfig, nil 83 } 84 85 func loadDatasourceConfigFile(file string) (*model.DatasourceConfig, error) { 86 logger.Infof("loading datasource config file: %s", file) 87 yamlBytes, err := os.ReadFile(file) 88 if err != nil { 89 return nil, fmt.Errorf("error on ReadFile: %w", err) 90 } 91 var datasourceConfig *model.DatasourceConfig 92 if err := yaml.UnmarshalStrict(yamlBytes, &datasourceConfig); err != nil { 93 return nil, fmt.Errorf("error on UnmarshalStrict: %w", err) 94 } 95 96 // default 97 if datasourceConfig.QueryTimeout == 0 { 98 datasourceConfig.QueryTimeout = 30 * time.Second 99 } 100 if datasourceConfig.Discovery.AnnotationKey == "" { 101 datasourceConfig.Discovery.AnnotationKey = "kuoss.org/datasource-type" 102 } 103 return datasourceConfig, nil 104 } 105 106 func loadUserConfigFile(file string) (*model.UserConfig, error) { 107 logger.Infof("loading user config file: %s", file) 108 yamlBytes, err := os.ReadFile(file) 109 if err != nil { 110 return nil, fmt.Errorf("error on ReadFile: %w", err) 111 } 112 var userConfig *model.UserConfig 113 if err := yaml.UnmarshalStrict(yamlBytes, &userConfig); err != nil { 114 return nil, fmt.Errorf("error on UnmarshalStrict: %w", err) 115 } 116 return userConfig, nil 117 } 118 119 func loadAlertingConfigFile(file string) (model.AlertingConfig, error) { 120 logger.Infof("loading alerting config file: %s", file) 121 yamlBytes, err := os.ReadFile(file) 122 if err != nil { 123 return model.AlertingConfig{}, fmt.Errorf("error on ReadFile: %w", err) 124 } 125 var alertingConfigFile *model.AlertingConfigFile 126 if err := yaml.UnmarshalStrict(yamlBytes, &alertingConfigFile); err != nil { 127 return model.AlertingConfig{}, fmt.Errorf("error on UnmarshalStrict: %w", err) 128 } 129 130 // default 131 alertingConfig := alertingConfigFile.AlertingConfig 132 if alertingConfig.EvaluationInterval == 0 { 133 alertingConfig.EvaluationInterval = 5 * time.Second 134 } 135 return alertingConfig, nil 136 }