Files
digital-psychology/apps/api/internal/httpserver/router.go
T
jackyu66gitandCursor 87b463b2bb feat(ECR-044): RhythmConfig 写面闭环并 Closed
复用 admin.explore.write、POST/PUT+审计、C端 GET /rhythm/configs、
H5 无 active 空态/失败回退;migration 000053。ExploreConfig Loop STOP,禁自动 ECR-045。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-13 20:43:08 +08:00

124 lines
5.3 KiB
Go

// Package httpserver wires HTTP routes for the 愈心谷 API.
package httpserver
import (
"context"
"log"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/yuxingu/digital-psychology/apps/api/internal/config"
"github.com/yuxingu/digital-psychology/apps/api/internal/handler"
"github.com/yuxingu/digital-psychology/apps/api/internal/llm/deepseek"
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
"github.com/yuxingu/digital-psychology/apps/api/internal/repository"
adminsvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/admin"
analyticssvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/analytics"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/ask"
authsvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/auth"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/bootstrap"
companionsvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/companion"
homesvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/home"
imagecardsvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/imagecard"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/membership"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/profile"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/relation"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/report"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/scale"
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
)
// NewRouter builds the Gin engine with all /api/v1 routes.
func NewRouter(pool *pgxpool.Pool, cfg config.Config) *gin.Engine {
profileRepo := &repository.ProfileRepo{Pool: pool}
reportRepo := &repository.ReportRepo{Pool: pool}
relationRepo := &repository.RelationRepo{Pool: pool}
askRepo := &repository.AskRepo{Pool: pool}
adminRepo := &repository.AdminRepo{Pool: pool}
analyticsRepo := &repository.AnalyticsRepo{Pool: pool}
authRepo := &repository.AuthRepo{Pool: pool}
var llm *deepseek.Client
if cfg.DeepSeek.Enabled() {
llm = deepseek.New(cfg.DeepSeek)
}
bootSvc := &bootstrap.Service{Profiles: profileRepo, Reports: reportRepo, Relations: relationRepo}
profileSvc := &profile.Service{Repo: profileRepo, Reports: reportRepo, Bootstrap: bootSvc}
reportSvc := &report.Service{
Profiles: profileRepo,
Reports: reportRepo,
Invites: &repository.SynastryInviteRepo{Pool: pool},
}
membershipSvc := &membership.Service{Reports: reportRepo}
relationSvc := &relation.Service{Profiles: profileRepo, Reports: reportRepo, Relations: relationRepo}
scaleRepo := &repository.ScaleRepo{Pool: pool}
scaleSvc := &scale.Service{Repo: scaleRepo, Profiles: profileRepo, Membership: membershipSvc}
askSvc := &ask.Service{Profiles: profileRepo, Reports: reportRepo, Ask: askRepo, LLM: llm}
companionSvc := &companionsvc.Service{Moods: &repository.MoodRepo{Pool: pool}}
imageCardSvc := &imagecardsvc.Service{
Profiles: profileRepo,
Reports: reportRepo,
Quotas: &repository.ImageCardRepo{Pool: pool},
}
homeSvc := &homesvc.Service{
Repo: &repository.HomeToolsRepo{Pool: pool},
Tips: &repository.HomeDailyTipsRepo{Pool: pool},
Profiles: profileRepo,
CMS: adminRepo,
LLM: llm,
}
analyticsSvc := &analyticssvc.Service{Repo: analyticsRepo}
adminSvc := &adminsvc.Service{
Repo: adminRepo, Reports: reportRepo, Home: homeSvc, Scales: scaleRepo,
}
authSvc := &authsvc.Service{Repo: authRepo, AvatarDir: "data/avatars"}
if err := adminSvc.EnsureBootstrap(context.Background(), adminsvc.BootstrapConfig{
Username: cfg.Admin.BootstrapUsername,
Password: cfg.Admin.BootstrapPassword,
}); err != nil {
log.Printf("admin bootstrap failed: %v", err)
}
r := gin.New()
r.Use(gin.Recovery(), gin.Logger(), middleware.RequestID())
r.Use(func(c *gin.Context) {
c.Header("Access-Control-Expose-Headers", "X-Device-Key, X-Request-Id")
c.Next()
})
api := r.Group("/api/v1")
handler.NewHealthHandler().Register(api)
api.GET("/ping", func(c *gin.Context) {
response.OK(c, gin.H{"pong": true})
})
(&handler.MediaHandler{AvatarDir: "data/avatars"}).Register(api)
(&handler.AdminHandler{Svc: adminSvc, Analytics: analyticsSvc}).Register(api)
authed := api.Group("")
authed.Use(middleware.DeviceAuth(pool))
(&handler.AuthHandler{Svc: authSvc}).Register(authed)
(&handler.AnalyticsHandler{Svc: analyticsSvc}).Register(authed)
(&handler.HomeHandler{Svc: homeSvc}).Register(authed)
(&handler.StarConfigPublicHandler{Repo: adminRepo}).Register(authed)
(&handler.RhythmConfigPublicHandler{Repo: adminRepo}).Register(authed)
gated := authed.Group("")
gated.Use(middleware.RequireRegistered(pool))
(&handler.ProfileHandler{Svc: profileSvc}).Register(gated)
(&handler.ReportHandler{Svc: reportSvc, Membership: membershipSvc}).Register(gated)
(&handler.SynastryHandler{Svc: reportSvc}).Register(gated)
(&handler.RelationHandler{Svc: relationSvc}).Register(gated)
(&handler.ScaleHandler{Svc: scaleSvc}).Register(gated)
(&handler.AskHandler{Svc: askSvc}).Register(gated)
(&handler.CompanionHandler{Svc: companionSvc}).Register(gated)
(&handler.ImageCardHandler{Svc: imageCardSvc}).Register(gated)
(&handler.ExploreHandler{Scales: scaleSvc}).Register(gated)
(&handler.GrowthHandler{
Plans: &repository.GrowthRepo{Pool: pool},
}).Register(gated)
return r
}