From ee6914e92fd10d40aee64310e4cbd7923f7c4423 Mon Sep 17 00:00:00 2001 From: zyj <18107291228@163.com> Date: Sat, 30 Aug 2025 18:37:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/index.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 config/index.go diff --git a/config/index.go b/config/index.go new file mode 100644 index 0000000..45d628a --- /dev/null +++ b/config/index.go @@ -0,0 +1,38 @@ +package config + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +type AppConfig struct { + Port int `yaml:"port"` + Database DatabaseConfig `yaml:"database"` + WorkerID int64 `yaml:"workerID"` + DatacenterID int64 `yaml:"datacenterID"` +} + +type DatabaseConfig struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + Host string `yaml:"host"` + Port int `yaml:"port"` + Database string `yaml:"database"` +} + +func LoadConfig() (*AppConfig, error) { + // 读取文件内容 + data, err := os.ReadFile("config.yaml") + if err != nil { + return nil, err + } + + // 解析 YAML + var cfg AppConfig + if err := yaml.Unmarshal(data, &cfg); err != nil { + return nil, err + } + + return &cfg, nil +}