feat(lyra): 搭建基础骨架与配置响应组件

- 使用 Viper 加载 TOML 配置
- 定义统一响应、业务错误码及响应辅助函数
- 添加分层职责约束并同步架构文档
This commit is contained in:
2026-10-16 23:36:34 +08:00
parent e4fc4af912
commit 8b320817ed
19 changed files with 418 additions and 22 deletions
@@ -0,0 +1,4 @@
# config 包
- 负责配置定义与加载。
- 具体配置项、来源与加载方式随实际需求确定,由程序入口调用配置加载逻辑。
@@ -0,0 +1,39 @@
package config
import (
"fmt"
"github.com/spf13/viper"
)
type Config struct {
App AppConfig `mapstructure:"app"`
HTTP HTTPConfig `mapstructure:"http"`
}
type AppConfig struct {
Name string `mapstructure:"name"`
Env string `mapstructure:"env"`
}
type HTTPConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
}
func Load(path string) (*Config, error) {
v := viper.New()
// 指定文件,按 .toml 扩展名识别格式
v.SetConfigFile(path)
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("读取配置失败: %w", err)
}
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("解析配置失败: %w", err)
}
return &cfg, nil
}