143 lines
3.8 KiB
Go
143 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"awesomeProject/internal/common"
|
|
"awesomeProject/internal/events"
|
|
"awesomeProject/internal/handlers"
|
|
"awesomeProject/internal/models"
|
|
"awesomeProject/internal/routers"
|
|
"awesomeProject/internal/tasks"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
// 1. 加载配置文件
|
|
config, err := common.LoadConfig("./configs/config.yaml")
|
|
if err != nil {
|
|
log.Fatalf("加载配置文件失败: %v", err)
|
|
}
|
|
log.Printf("配置加载成功: %s v%s", config.App.Name, config.App.Version)
|
|
|
|
// 2. 初始化数据库
|
|
if err := common.InitDatabase(&config.Database); err != nil {
|
|
log.Fatalf("数据库初始化失败: %v", err)
|
|
}
|
|
defer func() {
|
|
err := common.CloseDatabase()
|
|
if err != nil {
|
|
return
|
|
}
|
|
}()
|
|
|
|
// 4. 自动迁移数据表
|
|
if err := models.AutoMigrateAll(); err != nil {
|
|
log.Fatalf("数据表迁移失败: %v", err)
|
|
}
|
|
log.Println("数据表迁移完成")
|
|
|
|
// 5. 初始化系统配置
|
|
//configHandler := &handlers.AdminConfigHandler{}
|
|
//if err := configHandler.InitSystemConfigs(); err != nil {
|
|
// log.Fatalf("初始化系统配置失败: %v", err)
|
|
//}
|
|
//log.Println("系统配置初始化完成")
|
|
|
|
// 6. 初始化事件系统
|
|
events.InitEventSystem()
|
|
|
|
// 7. 初始化定时任务
|
|
tasks.InitCronTasks()
|
|
log.Println("定时任务初始化完成")
|
|
|
|
// 8. 初始化默认用户
|
|
initHandler := &handlers.InitHandler{}
|
|
if err := initHandler.InitAll(); err != nil {
|
|
log.Fatalf("初始化默认用户失败: %v", err)
|
|
}
|
|
log.Println("默认用户初始化完成")
|
|
|
|
// 9. 设置 Gin 模式
|
|
if config.App.Env == "production" {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
|
|
// 10. 创建Gin路由器和设置路由
|
|
router := routers.SetupRouter()
|
|
|
|
// 配置静态资源
|
|
// 上传文件的静态资源(允许缓存)
|
|
router.Static("/back/file", "./public/file")
|
|
|
|
// H5 静态资源
|
|
router.Static("/h5/static", "./public/h5/static")
|
|
router.Static("/h5/assets", "./public/h5/assets")
|
|
|
|
// Vue 静态资源(禁用缓存)
|
|
webGroup := router.Group("/")
|
|
webGroup.Use(func(c *gin.Context) {
|
|
// 只对web静态资源禁用缓存
|
|
if strings.HasPrefix(c.Request.URL.Path, "/assets") ||
|
|
strings.HasPrefix(c.Request.URL.Path, "/css") ||
|
|
strings.HasPrefix(c.Request.URL.Path, "/js") {
|
|
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
c.Header("Pragma", "no-cache")
|
|
c.Header("Expires", "0")
|
|
}
|
|
c.Next()
|
|
})
|
|
webGroup.Static("/assets", "./public/web/assets")
|
|
webGroup.Static("/css", "./public/web/css")
|
|
webGroup.Static("/js", "./public/web/js")
|
|
|
|
// H5 History 路由支持 - /h5 开头的路由返回 H5 的 index.html
|
|
router.NoRoute(func(c *gin.Context) {
|
|
path := c.Request.URL.Path
|
|
|
|
// 如果是 API 请求(/back 开头),返回 404
|
|
if strings.HasPrefix(path, "/back") {
|
|
c.JSON(404, gin.H{"error": "API not found"})
|
|
return
|
|
}
|
|
|
|
// H5 路由处理
|
|
if strings.HasPrefix(path, "/h5") {
|
|
// 检查是否是静态文件(包含文件扩展名)
|
|
if strings.Contains(path, ".") {
|
|
// 静态文件应该已经被上面的 Static 路由处理了
|
|
// 如果到这里说明文件不存在,返回 404
|
|
c.JSON(404, gin.H{"error": "File not found"})
|
|
return
|
|
}
|
|
// H5 路由返回 H5 的 index.html
|
|
c.File("./public/h5/index.html")
|
|
return
|
|
}
|
|
|
|
// 检查是否是静态文件(包含文件扩展名)
|
|
if strings.Contains(path, ".") {
|
|
// 设置禁用缓存的响应头
|
|
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
c.Header("Pragma", "no-cache")
|
|
c.Header("Expires", "0")
|
|
|
|
// 尝试访问 web 目录下的文件
|
|
c.File("./public/web" + path)
|
|
return
|
|
}
|
|
|
|
// 其他路由返回 Vue 应用
|
|
c.File("./public/web/index.html")
|
|
})
|
|
|
|
// 启动服务器
|
|
port := fmt.Sprintf(":%d", config.App.Port)
|
|
log.Printf("服务器启动在端口 %s", port)
|
|
if err := router.Run(port); err != nil {
|
|
log.Fatalf("服务器启动失败: %v", err)
|
|
}
|
|
|
|
}
|