feat(lyra): 搭建基础骨架与配置响应组件
- 使用 Viper 加载 TOML 配置 - 定义统一响应、业务错误码及响应辅助函数 - 添加分层职责约束并同步架构文档
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
# api 包
|
||||
|
||||
- 定义 HTTP 请求/响应 DTO,按资源分文件。
|
||||
- DTO 与内部模型分开:即使初期字段相近,也不把数据库模型直接暴露为响应,由 handler 层完成显式转换。
|
||||
- 不引用 handler、service、model,也不依赖 Gin,避免传输契约与实现耦合或循环引用。
|
||||
@@ -0,0 +1,21 @@
|
||||
package api
|
||||
|
||||
// ErrCode 错误码,前3位数表示错误类型,后3位数表示具体错误
|
||||
type ErrorCode int
|
||||
|
||||
const (
|
||||
CodeInvalidArgument ErrorCode = 10001
|
||||
CodeInternalError ErrorCode = 10002
|
||||
CodeLinkNotFound ErrorCode = 10003
|
||||
)
|
||||
|
||||
func (e ErrorCode) Message() string {
|
||||
switch e {
|
||||
case CodeInvalidArgument:
|
||||
return "请求参数不正确"
|
||||
case CodeLinkNotFound:
|
||||
return "链接不存在"
|
||||
default:
|
||||
return "服务暂时不可用"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package api
|
||||
|
||||
type Response struct {
|
||||
Success bool `json:"success"`
|
||||
Data any `json:"data,omitempty"`
|
||||
Error *ErrorInfo `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type ErrorInfo struct {
|
||||
Code ErrorCode `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Debug string `json:"debug,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user