Files
blind-select/backend/cmd/server/main.go
T
admin 06488f0237 Initial commit: 帮我选盲选应用
功能:
- Go后端 (Gin + GORM + PostgreSQL)
- UniApp用户端 (iOS/Android/小程序)
- DaisyUI5后台管理
- JWT认证 + 微信登录
- 盲选加权算法
- 会员系统 + 优惠券
- 打分评价 + 偏好学习
2026-06-08 20:18:31 +00:00

112 lines
3.3 KiB
Go

package main
import (
"log"
"github.com/blind-select/backend/internal/config"
"github.com/blind-select/backend/internal/database"
"github.com/blind-select/backend/internal/handler/v1"
"github.com/blind-select/backend/internal/middleware"
"github.com/gin-gonic/gin"
)
func main() {
cfg, err := config.Load("../config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
database.Init(cfg)
gin.SetMode(cfg.Server.Mode)
router := gin.New()
router.Use(gin.Recovery())
router.Use(middleware.CORSMiddleware())
authHandler := handler.NewAuthHandler(cfg.JWT.Secret, cfg.Wechat.AppID, cfg.Wechat.AppSecret)
// Public routes
v1 := router.Group("/api/v1")
{
auth := v1.Group("/auth")
{
auth.POST("/register", authHandler.Register)
auth.POST("/login", authHandler.Login)
auth.POST("/wechat/login", authHandler.WechatLogin)
auth.POST("/refresh", authHandler.RefreshToken)
}
user := v1.Group("/user")
user.Use(middleware.JWTAuth(cfg.JWT.Secret))
{
user.GET("/profile", authHandler.GetProfile)
user.PUT("/profile", authHandler.UpdateProfile)
}
blind := v1.Group("/blind")
blind.Use(middleware.JWTAuth(cfg.JWT.Secret))
{
blind.GET("/categories", authHandler.GetCategories)
blind.GET("/pool", authHandler.GetPool)
blind.POST("/choose", middleware.BlindDailyLimit(cfg.JWT.Secret), authHandler.Choose)
blind.GET("/result/:id", authHandler.GetResult)
blind.GET("/history", authHandler.GetHistory)
}
review := v1.Group("/review")
review.Use(middleware.JWTAuth(cfg.JWT.Secret))
{
review.POST("/submit", authHandler.SubmitReview)
review.GET("/stats", authHandler.GetReviewStats)
}
coupon := v1.Group("/coupon")
coupon.Use(middleware.JWTAuth(cfg.JWT.Secret))
{
coupon.GET("/list", authHandler.GetMyCoupons)
coupon.GET("/available", authHandler.GetAvailableCoupons)
coupon.POST("/claim", authHandler.ClaimCoupon)
}
member := v1.Group("/member")
member.Use(middleware.JWTAuth(cfg.JWT.Secret))
{
member.GET("/status", authHandler.GetMemberStatus)
member.GET("/plans", authHandler.GetMemberPlans)
member.POST("/subscribe", authHandler.SubscribeMember)
}
}
// Admin - public login
admin := router.Group("/api/v1/admin")
{
admin.POST("/login", authHandler.AdminLogin)
}
// Admin - authenticated
adminAuth := router.Group("/api/v1/admin")
adminAuth.Use(middleware.JWTAuth(cfg.JWT.Secret))
adminAuth.Use(middleware.AdminOnly(cfg.JWT.Secret))
{
adminAuth.GET("/dashboard", authHandler.Dashboard)
adminAuth.GET("/merchants", authHandler.ListMerchants)
adminAuth.POST("/merchants", authHandler.CreateMerchant)
adminAuth.PUT("/merchants/:id", authHandler.UpdateMerchant)
adminAuth.GET("/packages", authHandler.ListPackages)
adminAuth.POST("/packages", authHandler.CreatePackage)
adminAuth.PUT("/packages/:id", authHandler.UpdatePackage)
adminAuth.GET("/users", authHandler.ListUsers)
adminAuth.GET("/users/:id", authHandler.GetUserDetail)
adminAuth.GET("/reviews", authHandler.ListReviews)
adminAuth.GET("/coupons", authHandler.ListCoupons)
adminAuth.POST("/coupons", authHandler.CreateCoupon)
adminAuth.GET("/statistics", authHandler.GetStatistics)
adminAuth.GET("/export", authHandler.ExportData)
}
addr := ":" + cfg.Server.Port
log.Printf("Server starting on %s", addr)
if err := router.Run(addr); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}