-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (107 loc) · 3.03 KB
/
main.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"flag"
"fmt"
"net/http"
"os"
"MikuMikuCloudDrive/config"
"MikuMikuCloudDrive/core"
"MikuMikuCloudDrive/middleware"
"MikuMikuCloudDrive/models"
"MikuMikuCloudDrive/routes"
"MikuMikuCloudDrive/services"
"MikuMikuCloudDrive/utils/logger"
_ "MikuMikuCloudDrive/docs" // 导入生成的 docs 包
"github.com/fatih/color"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
func initDatabase() error {
db := core.InitGorm()
err := db.AutoMigrate(
&models.UserModel{},
&models.FileModel{},
&models.DirectoryModel{},
)
if err != nil {
logrus.Error("创建表结构失败:", err)
return err
}
logrus.Info("数据库表结构初始化成功")
return nil
}
// @title MikuMikuCloudDrive
// @version 1.0
// @description Gin实现的云盘后端文档
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name MIT LICENSE
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8888
// @securityDefinitions.apikey JWTAuth
// @in header
// @name Authorization
// @Security JWTAuth
func main() {
// 解析命令行参数
initDB := flag.Bool("initdb", false, "Initialize database tables")
flag.Parse()
// 初始化日志
logger.InitLogger(logrus.DebugLevel)
// 如果指定了--initdb参数,只初始化数据库
// main.go 文件已修改完成,现在支持通过 --initdb 参数初始化数据库表结构
if *initDB {
if err := initDatabase(); err != nil {
logrus.Error("数据库初始化失败:", err)
os.Exit(1)
}
return
}
app := config.ReadAppConfig()
r := gin.Default()
db := core.InitGorm()
rdb := core.InitRedis()
// 自动迁移数据库
if err := initDatabase(); err != nil {
logrus.Error("数据库初始化失败:", err)
os.Exit(1)
}
// 初始化服务上下文
svc := &services.ServiceContext{
DB: db,
RedisClient: rdb,
}
logrus.Debug("Gin 上下文创建成功!")
r.Use(cors.Default())
// 将服务上下文注入到 Gin 的上下文中
r.Use(func(c *gin.Context) {
c.Set("svc", svc)
c.Next()
})
// 注册路由
routes.UserRouter(r)
routes.FileRouter(r)
routes.DirectoryRoute(r)
// 注册静态文件
r.StaticFS("/web", http.Dir("./web"))
r.GET("/web", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/web/index.html")
})
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
// 获取配置文件中的上传目录
uploadDir := app.UploadDir
r.Use(middleware.CheckDirectoryAccess)
r.StaticFS("/uploads", http.Dir("./"+uploadDir))
// 加载模板文件
r.LoadHTMLGlob("templates/*")
logrus.Infof("%s[Ver %s] is running on %s:%d", color.GreenString(app.Title), color.BlackString(app.Version), app.Server, app.Port)
if err := r.Run(fmt.Sprintf("%s:%d", app.Server, app.Port)); err != nil {
logrus.Error("服务器启动失败:", err)
os.Exit(1)
}
}