-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (85 loc) · 2.19 KB
/
main.go
File metadata and controls
100 lines (85 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"Activity/api"
"Activity/config"
"Activity/models"
"Activity/storage/mysql"
"Activity/storage/mysql/repository"
"context"
"fmt"
"log"
"github.com/gin-gonic/gin"
)
func main() {
// 加载配置
cfg, err := config.LoadConfig("config/config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// 设置gin模式
gin.SetMode(cfg.API.Mode)
// 初始化数据库连接
db, err := mysql.NewDB(&mysql.Config{
Host: cfg.MySQL.Host,
Port: cfg.MySQL.Port,
User: cfg.MySQL.User,
Password: cfg.MySQL.Password,
Database: cfg.MySQL.Database,
})
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
// 创建仓储实例
activityRepo := repository.NewActivityRepository(db)
// 创建服务实例
activityService := api.NewActivityService(activityRepo)
gameService := api.NewGameService(activityRepo)
// 创建处理器
handler := api.NewHandler(gameService, activityService)
// 创建路由
r := gin.Default()
// 注册Swagger路由
handler.RegisterSwagger(r)
// 注册API路由
handler.RegisterRoutes(r)
// 启动服务
addr := fmt.Sprintf(":%d", cfg.API.Port)
log.Printf("Server starting on %s", addr)
if err := r.Run(addr); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
// ActivityRepository 活动仓库实现
type activityRepository struct {
// TODO: 添加数据库连接等依赖
}
func NewActivityRepository() api.ActivityRepository {
return &activityRepository{}
}
func (r *activityRepository) GetActivity(ctx context.Context, activityID string) (models.ActivityInterface, error) {
// TODO: 从数据库获取活动信息
// 这里先返回一个模拟数据
return &models.CommunityActivity{
MetaActivity: models.MetaActivity{
ID: 1,
Category: "community",
Version: "v1",
StartAt: 1679000000,
EndAt: 1679086400,
Status: 1,
},
GameList: []models.GameInterface{
&models.CommunityPostGame{
Name_: "发帖奖励",
Prize: &models.DiscountCodePrize{
DiscountCode: "COMMUNITY_2024",
PriceRuleID: 123,
Probability: 100,
TotalNum: 1000,
RemainNum: 1000,
},
State: models.GameStateOPEN,
},
},
}, nil
}