|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "gopkg.in/yaml.v3" |
| 8 | +) |
| 9 | + |
| 10 | +// yamlConfig YAML 配置结构 |
| 11 | +type yamlConfig struct { |
| 12 | + Global globalConfig `yaml:"global"` |
| 13 | + Variables []variableConfig `yaml:"variables"` |
| 14 | + Targets targetList `yaml:"targets"` |
| 15 | + Loggers []loggerConfig `yaml:"loggers"` |
| 16 | +} |
| 17 | + |
| 18 | +type globalConfig struct { |
| 19 | + IsLog bool `yaml:"isLog"` |
| 20 | + ChanSize int `yaml:"chanSize"` |
| 21 | + InnerLogPath string `yaml:"innerLogPath"` |
| 22 | + InnerLogEncode string `yaml:"innerLogEncode"` |
| 23 | +} |
| 24 | + |
| 25 | +type variableConfig struct { |
| 26 | + Name string `yaml:"name"` |
| 27 | + Value string `yaml:"value"` |
| 28 | +} |
| 29 | + |
| 30 | +type targetList struct { |
| 31 | + File []fileTargetConfig `yaml:"file"` |
| 32 | + Udp []udpTargetConfig `yaml:"udp"` |
| 33 | + Http []httpTargetConfig `yaml:"http"` |
| 34 | + EMail []emailTargetConfig `yaml:"email"` |
| 35 | + Fmt []fmtTargetConfig `yaml:"fmt"` |
| 36 | +} |
| 37 | + |
| 38 | +type fileTargetConfig struct { |
| 39 | + Name string `yaml:"name"` |
| 40 | + IsLog bool `yaml:"isLog"` |
| 41 | + Layout string `yaml:"layout"` |
| 42 | + Encode string `yaml:"encode"` |
| 43 | + FileMaxSize int64 `yaml:"fileMaxSize"` |
| 44 | + FileName string `yaml:"fileName"` |
| 45 | +} |
| 46 | + |
| 47 | +type udpTargetConfig struct { |
| 48 | + Name string `yaml:"name"` |
| 49 | + IsLog bool `yaml:"isLog"` |
| 50 | + Layout string `yaml:"layout"` |
| 51 | + Encode string `yaml:"encode"` |
| 52 | + RemoteIP string `yaml:"remoteIP"` |
| 53 | +} |
| 54 | + |
| 55 | +type httpTargetConfig struct { |
| 56 | + Name string `yaml:"name"` |
| 57 | + IsLog bool `yaml:"isLog"` |
| 58 | + Layout string `yaml:"layout"` |
| 59 | + Encode string `yaml:"encode"` |
| 60 | + HttpUrl string `yaml:"httpUrl"` |
| 61 | +} |
| 62 | + |
| 63 | +type emailTargetConfig struct { |
| 64 | + Name string `yaml:"name"` |
| 65 | + IsLog bool `yaml:"isLog"` |
| 66 | + Layout string `yaml:"layout"` |
| 67 | + Encode string `yaml:"encode"` |
| 68 | + MailServer string `yaml:"mailServer"` |
| 69 | + MailAccount string `yaml:"mailAccount"` |
| 70 | + MailNickName string `yaml:"mailNickName"` |
| 71 | + MailPassword string `yaml:"mailPassword"` |
| 72 | + ToMail string `yaml:"toMail"` |
| 73 | + Subject string `yaml:"subject"` |
| 74 | +} |
| 75 | + |
| 76 | +type fmtTargetConfig struct { |
| 77 | + Name string `yaml:"name"` |
| 78 | + IsLog bool `yaml:"isLog"` |
| 79 | + Layout string `yaml:"layout"` |
| 80 | + Encode string `yaml:"encode"` |
| 81 | +} |
| 82 | + |
| 83 | +type loggerConfig struct { |
| 84 | + Name string `yaml:"name"` |
| 85 | + IsLog bool `yaml:"isLog"` |
| 86 | + Layout string `yaml:"layout"` |
| 87 | + ConfigMode string `yaml:"configMode"` |
| 88 | + Levels []loggerLevelConfig `yaml:"levels"` |
| 89 | +} |
| 90 | + |
| 91 | +type loggerLevelConfig struct { |
| 92 | + Level string `yaml:"level"` |
| 93 | + Targets string `yaml:"targets"` |
| 94 | + IsLog bool `yaml:"isLog"` |
| 95 | +} |
| 96 | + |
| 97 | +// LoadYamlConfig loads configuration from YAML file |
| 98 | +func LoadYamlConfig(configFile string) (*AppConfig, error) { |
| 99 | + data, err := os.ReadFile(configFile) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed to read config file: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + var yc yamlConfig |
| 105 | + if err := yaml.Unmarshal(data, &yc); err != nil { |
| 106 | + return nil, fmt.Errorf("failed to parse YAML config: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + // Convert YAML config to AppConfig |
| 110 | + appConfig := &AppConfig{ |
| 111 | + Global: &GlobalConfig{ |
| 112 | + IsLog: yc.Global.IsLog, |
| 113 | + ChanSize: yc.Global.ChanSize, |
| 114 | + InnerLogPath: yc.Global.InnerLogPath, |
| 115 | + InnerLogEncode: yc.Global.InnerLogEncode, |
| 116 | + }, |
| 117 | + Variables: make([]*VariableConfig, len(yc.Variables)), |
| 118 | + Loggers: make([]*LoggerConfig, len(yc.Loggers)), |
| 119 | + Targets: &TargetList{ |
| 120 | + FileTargets: make([]*FileTargetConfig, len(yc.Targets.File)), |
| 121 | + UdpTargets: make([]*UdpTargetConfig, len(yc.Targets.Udp)), |
| 122 | + HttpTargets: make([]*HttpTargetConfig, len(yc.Targets.Http)), |
| 123 | + EMailTargets: make([]*EMailTargetConfig, len(yc.Targets.EMail)), |
| 124 | + FmtTargets: make([]*FmtTargetConfig, len(yc.Targets.Fmt)), |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + // Convert variables |
| 129 | + for i, v := range yc.Variables { |
| 130 | + appConfig.Variables[i] = &VariableConfig{Name: v.Name, Value: v.Value} |
| 131 | + } |
| 132 | + |
| 133 | + // Convert loggers |
| 134 | + for i, l := range yc.Loggers { |
| 135 | + levels := make([]*LoggerLevelConfig, len(l.Levels)) |
| 136 | + for j, level := range l.Levels { |
| 137 | + levels[j] = &LoggerLevelConfig{ |
| 138 | + Level: level.Level, |
| 139 | + Targets: level.Targets, |
| 140 | + IsLog: level.IsLog, |
| 141 | + } |
| 142 | + } |
| 143 | + appConfig.Loggers[i] = &LoggerConfig{ |
| 144 | + Name: l.Name, |
| 145 | + IsLog: l.IsLog, |
| 146 | + Layout: l.Layout, |
| 147 | + ConfigMode: l.ConfigMode, |
| 148 | + Levels: levels, |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Convert file targets |
| 153 | + for i, t := range yc.Targets.File { |
| 154 | + appConfig.Targets.FileTargets[i] = &FileTargetConfig{ |
| 155 | + Name: t.Name, |
| 156 | + IsLog: t.IsLog, |
| 157 | + Layout: t.Layout, |
| 158 | + Encode: t.Encode, |
| 159 | + FileMaxSize: t.FileMaxSize, |
| 160 | + FileName: t.FileName, |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // Convert UDP targets |
| 165 | + for i, t := range yc.Targets.Udp { |
| 166 | + appConfig.Targets.UdpTargets[i] = &UdpTargetConfig{ |
| 167 | + Name: t.Name, |
| 168 | + IsLog: t.IsLog, |
| 169 | + Layout: t.Layout, |
| 170 | + Encode: t.Encode, |
| 171 | + RemoteIP: t.RemoteIP, |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Convert HTTP targets |
| 176 | + for i, t := range yc.Targets.Http { |
| 177 | + appConfig.Targets.HttpTargets[i] = &HttpTargetConfig{ |
| 178 | + Name: t.Name, |
| 179 | + IsLog: t.IsLog, |
| 180 | + Layout: t.Layout, |
| 181 | + Encode: t.Encode, |
| 182 | + HttpUrl: t.HttpUrl, |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // Convert Email targets |
| 187 | + for i, t := range yc.Targets.EMail { |
| 188 | + appConfig.Targets.EMailTargets[i] = &EMailTargetConfig{ |
| 189 | + Name: t.Name, |
| 190 | + IsLog: t.IsLog, |
| 191 | + Layout: t.Layout, |
| 192 | + Encode: t.Encode, |
| 193 | + MailServer: t.MailServer, |
| 194 | + MailAccount: t.MailAccount, |
| 195 | + MailNickName: t.MailNickName, |
| 196 | + MailPassword: t.MailPassword, |
| 197 | + ToMail: t.ToMail, |
| 198 | + Subject: t.Subject, |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + // Convert Fmt targets |
| 203 | + for i, t := range yc.Targets.Fmt { |
| 204 | + appConfig.Targets.FmtTargets[i] = &FmtTargetConfig{ |
| 205 | + Name: t.Name, |
| 206 | + IsLog: t.IsLog, |
| 207 | + Layout: t.Layout, |
| 208 | + Encode: t.Encode, |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + return appConfig, nil |
| 213 | +} |
0 commit comments