Files
digital-psychology/apps/api/internal/httpserver/router.go
T
jackyu66gitandCursor 879bf70cb7
ci / h5 (push) Canceled after 0s
ci / api (push) Canceled after 0s
ci / ess-docs (push) Canceled after 0s
feat(ECR-006): 落地运营后台 Phase A(admin API + admin-h5)
新增独立鉴权的 /api/v1/admin 与 Vue 控制台;会员授予与审计同事务,并补集成/单测。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-06 18:35:53 +08:00

96 lines
3.9 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"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/ask"
companionsvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/companion"
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}
var llm *deepseek.Client
if cfg.DeepSeek.Enabled() {
llm = deepseek.New(cfg.DeepSeek)
}
profileSvc := &profile.Service{Repo: profileRepo}
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}
scaleSvc := &scale.Service{Repo: &repository.ScaleRepo{Pool: pool}, Profiles: profileRepo}
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},
}
adminSvc := &adminsvc.Service{Repo: adminRepo, Reports: reportRepo}
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.AdminHandler{Svc: adminSvc}).Register(api)
authed := api.Group("")
authed.Use(middleware.DeviceAuth(pool))
(&handler.ProfileHandler{Svc: profileSvc}).Register(authed)
(&handler.ReportHandler{Svc: reportSvc, Membership: membershipSvc}).Register(authed)
(&handler.SynastryHandler{Svc: reportSvc}).Register(authed)
(&handler.RelationHandler{Svc: relationSvc}).Register(authed)
(&handler.ScaleHandler{Svc: scaleSvc}).Register(authed)
(&handler.AskHandler{Svc: askSvc}).Register(authed)
(&handler.CompanionHandler{Svc: companionSvc}).Register(authed)
(&handler.ImageCardHandler{Svc: imageCardSvc}).Register(authed)
(&handler.ExploreHandler{}).Register(authed)
(&handler.GrowthHandler{
Plans: &repository.GrowthRepo{Pool: pool},
}).Register(authed)
return r
}