feat(expiry): 完成 #20 模块目录与健康路由

This commit is contained in:
root
2026-03-04 17:06:26 +08:00
parent fe9f64f51d
commit f092019c61
7 changed files with 109 additions and 1 deletions
+28
View File
@@ -0,0 +1,28 @@
package expiry
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Handler 负责 HTTP 层处理。
type Handler struct {
service *Service
}
func NewHandler(service *Service) *Handler {
return &Handler{service: service}
}
// Healthz 用于 expiry 模块健康检查。
func (h *Handler) Healthz(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": gin.H{
"module": "expiry",
"status": "ok",
},
})
}
+19
View File
@@ -0,0 +1,19 @@
package expiry
import "time"
// ExpiryItem 表示保质期物品。
// 详细字段会在后续 issue 中完善。
type ExpiryItem struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// ExpiryUserSettings 表示用户提醒设置。
// 详细字段会在后续 issue 中完善。
type ExpiryUserSettings struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
+12
View File
@@ -0,0 +1,12 @@
package expiry
import "gorm.io/gorm"
// Repository 封装保质期模块的数据访问能力。
type Repository struct {
db *gorm.DB
}
func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
+10
View File
@@ -0,0 +1,10 @@
package expiry
// Service 封装保质期模块业务逻辑。
type Service struct {
repo *Repository
}
func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
+19
View File
@@ -0,0 +1,19 @@
package routes
import (
"github.com/gin-gonic/gin"
expiryhandler "wx_service/internal/expiry"
)
func registerExpiryRoutes(protected *gin.RouterGroup, expiryHandler *expiryhandler.Handler) {
if expiryHandler == nil {
return
}
// 后续 issue 会补齐完整的物品/设置接口。
expiry := protected.Group("")
{
_ = expiry
}
}
+15
View File
@@ -10,6 +10,7 @@ import (
qiniuhandler "wx_service/internal/common/qiniu/handler"
rediscache "wx_service/internal/common/redis/cache"
oahandler "wx_service/internal/common/wechat_official/handler"
expiryhandler "wx_service/internal/expiry"
lawyerhandler "wx_service/internal/lawyer/handler"
membershiphandler "wx_service/internal/membership/handler"
"wx_service/internal/middleware"
@@ -28,6 +29,7 @@ func Register(
oaOAuthHandler *oahandler.OAuthHandler,
sessionCache *rediscache.SessionUserCache,
lawyerHandler *lawyerhandler.LawyerHandler,
expiryHandler *expiryhandler.Handler,
) {
// Register 用来集中注册所有 HTTP 路由,便于工程结构更清晰:
// - main 只负责初始化(配置/DB/依赖注入)
@@ -57,6 +59,19 @@ func Register(
}
}
// 保质期提醒模块使用独立前缀 /api/expiry,与现有 /api/v1 并存。
expiryAPI := router.Group("/api/expiry")
{
expiryAPI.GET("/healthz", expiryHandler.Healthz)
expiryProtected := expiryAPI.Group("")
expiryProtected.Use(middleware.AuthMiddleware(db, sessionCache))
expiryProtected.Use(middleware.RequireUserMiddleware())
{
registerExpiryRoutes(expiryProtected, expiryHandler)
}
}
// 健康检查:用于容器/负载均衡探活
router.GET("/healthz", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})