Documentation
¶
Overview ¶
Package common 提供七层架构的基础接口定义,规范 Entity、Manager、Repository、Service、Controller、Middleware 和 ConfigMgr 的行为契约。
核心特性:
- 七层架构基础接口:定义各层的基础接口类型,确保架构一致性
- 生命周期管理:提供统一的 OnStart 和 OnStop 钩子方法
- 命名规范:每层接口要求实现对应的名称方法,便于调试和日志
- 行为契约:通过接口定义各层的核心行为,建立分层依赖关系
- 依赖注入支持:为依赖注入容器提供标准接口类型
基本用法:
// 实现 Entity 接口
type User struct {
ID string `gorm:"primaryKey"`
Name string
}
func (u *User) EntityName() string { return "User" }
func (u *User) TableName() string { return "users" }
func (u *User) GetId() string { return u.ID }
// 实现 Service 接口
type UserService struct {}
func (s *UserService) ServiceName() string { return "UserService" }
func (s *UserService) OnStart() error { return nil }
func (s *UserService) OnStop() error { return nil }
接口层次:
各层之间有明确的依赖关系,从低到高依次为: ConfigMgr → Entity → Manager → Repository → Service → Controller/Middleware 上层可以依赖下层,下层不能依赖上层。
Index ¶
- Constants
- func GetMap(value any) (map[string]any, error)
- func GetMapOrDefault(value any, defaultValue map[string]any) map[string]any
- func GetString(value any) (string, error)
- func GetStringOrDefault(value any, defaultValue string) string
- type IBaseController
- type IBaseEntity
- type IBaseListener
- type IBaseManager
- type IBaseMiddleware
- type IBaseRepository
- type IBaseScheduler
- type IBaseService
- type IMessageListener
- type ISubscribeOption
Constants ¶
const ( HTTPStatusContinue = 100 // HTTP/1.1: Continue HTTPStatusSwitchingProtocols = 101 // HTTP/1.1: Switching Protocols HTTPStatusOK = 200 // HTTP/1.1: OK HTTPStatusCreated = 201 // HTTP/1.1: Created HTTPStatusAccepted = 202 // HTTP/1.1: Accepted HTTPStatusNonAuthoritativeInfo = 203 // HTTP/1.1: Non-Authoritative Information HTTPStatusNoContent = 204 // HTTP/1.1: No Content HTTPStatusResetContent = 205 // HTTP/1.1: Reset Content HTTPStatusPartialContent = 206 // HTTP/1.1: Partial Content HTTPStatusMultipleChoices = 300 // HTTP/1.1: Multiple Choices HTTPStatusMovedPermanently = 301 // HTTP/1.1: Moved Permanently HTTPStatusFound = 302 // HTTP/1.1: Found HTTPStatusSeeOther = 303 // HTTP/1.1: See Other HTTPStatusNotModified = 304 // HTTP/1.1: Not Modified HTTPStatusUseProxy = 305 // HTTP/1.1: Use Proxy HTTPStatusTemporaryRedirect = 307 // HTTP/1.1: Temporary Redirect HTTPStatusPermanentRedirect = 308 // HTTP/1.1: Permanent Redirect HTTPStatusBadRequest = 400 // HTTP/1.1: Bad Request HTTPStatusPaymentRequired = 402 // HTTP/1.1: Payment Required HTTPStatusForbidden = 403 // HTTP/1.1: Forbidden HTTPStatusNotFound = 404 // HTTP/1.1: Not Found HTTPStatusMethodNotAllowed = 405 // HTTP/1.1: Method Not Allowed HTTPStatusNotAcceptable = 406 // HTTP/1.1: Not Acceptable HTTPStatusProxyAuthRequired = 407 // HTTP/1.1: Proxy Authentication Required HTTPStatusRequestTimeout = 408 // HTTP/1.1: Request Timeout HTTPStatusConflict = 409 // HTTP/1.1: Conflict HTTPStatusGone = 410 // HTTP/1.1: Gone HTTPStatusLengthRequired = 411 // HTTP/1.1: Length Required HTTPStatusPreconditionFailed = 412 // HTTP/1.1: Precondition Failed HTTPStatusPayloadTooLarge = 413 // HTTP/1.1: Payload Too Large HTTPStatusURITooLong = 414 // HTTP/1.1: URI Too Long HTTPStatusUnsupportedMediaType = 415 // HTTP/1.1: Unsupported Media Type HTTPStatusRangeNotSatisfiable = 416 // HTTP/1.1: Range Not Satisfiable HTTPStatusExpectationFailed = 417 // HTTP/1.1: Expectation Failed HTTPStatusTeapot = 418 // HTTP/1.1: I'm a teapot HTTPStatusMisdirectedRequest = 421 // HTTP/1.1: Misdirected Request HTTPStatusUnprocessableEntity = 422 // HTTP/1.1: Unprocessable Entity HTTPStatusLocked = 423 // HTTP/1.1: Locked HTTPStatusFailedDependency = 424 // HTTP/1.1: Failed Dependency HTTPStatusTooEarly = 425 // HTTP/1.1: Too Early HTTPStatusUpgradeRequired = 426 // HTTP/1.1: Upgrade Required HTTPStatusPreconditionRequired = 428 // HTTP/1.1: Precondition Required HTTPStatusTooManyRequests = 429 // HTTP/1.1: Too Many Requests HTTPStatusRequestHeaderFieldsTooLarge = 431 // HTTP/1.1: Request Header Fields Too Large HTTPStatusInternalServerError = 500 // HTTP/1.1: Internal Server Error HTTPStatusNotImplemented = 501 // HTTP/1.1: Not Implemented HTTPStatusBadGateway = 502 // HTTP/1.1: Bad Gateway HTTPStatusGatewayTimeout = 504 // HTTP/1.1: Gateway Timeout HTTPStatusHTTPVersionNotSupported = 505 // HTTP/1.1: HTTP Version Not Supported HTTPStatusVariantAlsoNegotiates = 506 // HTTP/1.1: Variant Also Negotiates HTTPStatusInsufficientStorage = 507 // HTTP/1.1: Insufficient Storage HTTPStatusLoopDetected = 508 // HTTP/1.1: Loop Detected HTTPStatusNotExtended = 510 // HTTP/1.1: Not Extended HTTPStatusNetworkAuthenticationRequired = 511 // HTTP/1.1: Network Authentication Required )
Variables ¶
This section is empty.
Functions ¶
func GetMapOrDefault ¶ added in v0.0.6
GetMapOrDefault 从 any 类型中安全获取 map[string]any 值,失败时返回默认值
func GetStringOrDefault ¶ added in v0.0.6
GetStringOrDefault 从 any 类型中安全获取字符串值,失败时返回默认值
Types ¶
type IBaseController ¶
type IBaseController interface {
// ControllerName 返回当前控制器的类名
ControllerName() string
// GetRouter 返回当前控制器的路由
// 路由格式同OpenAPI @Router 规范
// 如 `/aaa/bbb [GET]`; `/aaa/bbb [POST]`
GetRouter() string
// Handle 处理当前控制器的请求
Handle(ctx *gin.Context)
}
IBaseController 基础控制器接口 所有 Controller 类必须继承此接口并实现相关方法 用于定义基础控制器的规范,包括路由和处理函数。
type IBaseEntity ¶
type IBaseEntity interface {
// EntityName 返回当前实体实现的类名
// 用于标识和调试实体实例
EntityName() string
// TableName 返回当前实体的表名
// 用于数据库操作
TableName() string
// GetId 返回实体的唯一标识
// 用于实体的索引和检索
GetId() string
}
IBaseEntity 实体基类接口 所有 Entity 类必须继承此接口并实现 EntityName 和 GetId 方法 系统通过此接口判断是否符合标准实体定义
type IBaseListener ¶ added in v0.0.7
type IBaseListener interface {
// ListenerName 返回监听器名称
// 格式:xxxListenerImpl(小驼峰,带 Impl 后缀)
ListenerName() string
// GetQueue 返回监听的队列名称
// 返回值示例:"message.created", "user.registered"
GetQueue() string
// GetSubscribeOptions 返回订阅选项
// 可配置是否持久化、是否自动确认、并发消费者数量等
GetSubscribeOptions() []ISubscribeOption
// Handle 处理队列消息
// ctx: 上下文
// msg: 消息对象,包含 ID、Body、Headers
// 返回: 处理错误(返回 error 会触发 Nack)
Handle(ctx context.Context, msg IMessageListener) error
// OnStart 在服务器启动时触发
OnStart() error
// OnStop 在服务器停止时触发
OnStop() error
}
IBaseListener 基础监听器接口 所有 Listener 类必须继承此接口并实现相关方法 用于定义监听器的基础行为和契约
type IBaseManager ¶
type IBaseManager interface {
// ManagerName 返回管理器名称
ManagerName() string
// Health 检查管理器健康状态
Health() error
// OnStart 在服务器启动时触发
OnStart() error
// OnStop 在服务器停止时触发
OnStop() error
}
IBaseManager 管理器基础接口定义
type IBaseMiddleware ¶
type IBaseMiddleware interface {
// MiddlewareName 返回中间件的名称
MiddlewareName() string
// Order 返回中间件的执行顺序
Order() int
// Wrapper 返回一个中间件函数,用于包装请求处理函数
Wrapper() gin.HandlerFunc
// OnStart 在服务器启动时触发
OnStart() error
// OnStop 在服务器停止时触发
OnStop() error
}
IBaseMiddleware 基础中间件接口 所有 Middleware 类必须继承此接口并实现相关方法 用于定义基础中间件的规范,包括名称、执行顺序和包装函数。
type IBaseRepository ¶
type IBaseRepository interface {
// RepositoryName 返回当前存储库实现的类名
// 用于标识和调试存储库实例
RepositoryName() string
// OnStart 在服务器启动时触发
OnStart() error
// OnStop 在服务器停止时触发
OnStop() error
}
IBaseRepository 存储库基类接口 所有 Repository 类必须继承此接口并实现 RepositoryName 方法 系统通过此接口判断是否符合标准存储层定义
type IBaseScheduler ¶ added in v0.0.7
type IBaseScheduler interface {
// SchedulerName 返回定时器名称
// 格式:xxxScheduler(小驼峰)
// 示例:"cleanupScheduler"
SchedulerName() string
// GetRule 返回 Crontab 定时规则
// 使用标准 6 段式格式:秒 分 时 日 月 周
// 示例:"0 */5 * * * *" 表示每 5 分钟执行一次
// "0 0 2 * * *" 表示每天凌晨 2 点执行
// "0 0 * * * 1" 表示每周一凌晨执行
GetRule() string
// GetTimezone 返回定时器使用的时区
// 返回空字符串时使用服务器本地时间
// 支持标准时区名称,如 "Asia/Shanghai", "UTC", "America/New_York"
// 默认值:空字符串(服务器本地时间)
GetTimezone() string
// OnTick 定时触发时调用
// tickID: 计划执行时间的 Unix 时间戳(秒级),可用于去重或日志追踪
// 返回: 执行错误(返回 error 不会触发重试,仅记录日志)
OnTick(tickID int64) error
// OnStart 在服务器启动时触发
// 用于初始化定时器状态、连接资源等
OnStart() error
// OnStop 在服务器停止时触发
// 用于清理资源、保存状态等
OnStop() error
}
IBaseScheduler 基础定时器接口 所有 Scheduler 类必须继承此接口并实现相关方法 用于定义定时器的基础行为和契约
type IBaseService ¶
type IBaseService interface {
// ServiceName 返回当前服务实现的类名
// 用于标识和调试服务实例
ServiceName() string
// OnStart 在服务器启动时触发
OnStart() error
// OnStop 在服务器停止时触发
OnStop() error
}
IBaseService 服务基类接口 所有 Service 类必须继承此接口并实现 GetServiceName 方法 系统通过此接口判断是否符合标准服务定义