diff --git a/middleware/static.go b/middleware/static.go new file mode 100644 index 0000000..4c6bfc7 --- /dev/null +++ b/middleware/static.go @@ -0,0 +1,18 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "strings" +) + +// StaticCacheMiddleware add embed file static cache +func StaticCacheMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + // Apply the Cache-Control header to the static files + if strings.HasPrefix(c.Request.URL.Path, "/assets/") { + c.Header("Cache-Control", "private, max-age=86400") + } + // Continue to the next middleware or handler + c.Next() + } +} diff --git a/routers/router.go b/routers/router.go index 4a09998..4f58793 100644 --- a/routers/router.go +++ b/routers/router.go @@ -12,72 +12,74 @@ import ( ) // AppendCors 添加是否跨域(debug模式开启) -func AppendCors(r *gin.Engine) { +func AppendCors(app *gin.Engine) { if setting.ServerSetting.RunMode == "debug" { - r.Use(middleware.Cors()) + app.Use(middleware.Cors()) } } // ServerStaticHtml 启用返回打包的静态文件 -func AppendServerStaticHtml(r *gin.Engine, f embed.FS) { +func AppendServerStaticHtml(app *gin.Engine, f embed.FS) { if setting.ServerSetting.EmbedHtml == "disable" { return } + app.Use(middleware.StaticCacheMiddleware()) + assets, _ := fs.Sub(f, "web/dist/assets") dist, _ := fs.Sub(f, "web/dist") - r.StaticFS("assets/", http.FS(assets)) - r.GET("/", func(ctx *gin.Context) { + app.StaticFS("assets/", http.FS(assets)) + app.GET("/", func(ctx *gin.Context) { ctx.FileFromFS("/", http.FS(dist)) }) } -// InitRouter initialize routing information +// InitRouter 初始化路由 func InitRouter(f embed.FS) *gin.Engine { - r := gin.New() - r.Use(middleware.LogMiddleware()) - r.Use(gin.Recovery()) + app := gin.New() + app.Use(middleware.LogMiddleware()) + app.Use(gin.Recovery()) - AppendCors(r) - AppendServerStaticHtml(r, f) + AppendCors(app) + AppendServerStaticHtml(app, f) - r.POST("/auth", api.GetAuth) - apiv1 := r.Group("/api/v1") - apiv1.Use(middleware.JWT()) + app.POST("/auth", api.GetAuth) + apiV1 := app.Group("/api/v1") + apiV1.Use(middleware.JWT()) { // sendways - apiv1.POST("/sendways/add", v1.AddMsgSendWay) - apiv1.POST("/sendways/delete", v1.DeleteMsgSendWay) - apiv1.POST("/sendways/edit", v1.EditSendWay) - apiv1.POST("/sendways/test", v1.TestSendWay) - apiv1.GET("/sendways/list", v1.GetMsgSendWayList) - apiv1.GET("/sendways/get", v1.GetMsgSendWay) + apiV1.POST("/sendways/add", v1.AddMsgSendWay) + apiV1.POST("/sendways/delete", v1.DeleteMsgSendWay) + apiV1.POST("/sendways/edit", v1.EditSendWay) + apiV1.POST("/sendways/test", v1.TestSendWay) + apiV1.GET("/sendways/list", v1.GetMsgSendWayList) + apiV1.GET("/sendways/get", v1.GetMsgSendWay) // sendtasks - apiv1.GET("/sendtasks/list", v1.GetMsgSendTaskList) - apiv1.POST("/sendtasks/add", v1.AddMsgSendTask) - apiv1.POST("/sendtasks/delete", v1.DeleteMsgSendTask) - apiv1.POST("/sendtasks/edit", v1.EditMsgSendTask) + apiV1.GET("/sendtasks/list", v1.GetMsgSendTaskList) + apiV1.POST("/sendtasks/add", v1.AddMsgSendTask) + apiV1.POST("/sendtasks/delete", v1.DeleteMsgSendTask) + apiV1.POST("/sendtasks/edit", v1.EditMsgSendTask) // sendtasks/ins - apiv1.POST("/sendtasks/ins/addmany", v1.AddManyTasksIns) - apiv1.POST("/sendtasks/ins/addone", v1.AddTasksIns) - apiv1.GET("/sendtasks/ins/gettask", v1.GetMsgSendWayIns) - apiv1.POST("/sendtasks/ins/delete", v1.DeleteMsgTaskIns) + apiV1.POST("/sendtasks/ins/addmany", v1.AddManyTasksIns) + apiV1.POST("/sendtasks/ins/addone", v1.AddTasksIns) + apiV1.GET("/sendtasks/ins/gettask", v1.GetMsgSendWayIns) + apiV1.POST("/sendtasks/ins/delete", v1.DeleteMsgTaskIns) // message/send - apiv1.POST("/message/send", v1.DoSendMassage) + apiV1.POST("/message/send", v1.DoSendMassage) - apiv1.GET("/sendlogs/list", v1.GetTaskSendLogsList) + apiV1.GET("/sendlogs/list", v1.GetTaskSendLogsList) // settings - apiv1.POST("/settings/setpasswd", v1.EditPasswd) - apiv1.POST("/settings/set", v1.EditSettings) - apiv1.GET("/settings/getsetting", v1.GetUserSetting) + apiV1.POST("/settings/setpasswd", v1.EditPasswd) + apiV1.POST("/settings/set", v1.EditSettings) + apiV1.GET("/settings/getsetting", v1.GetUserSetting) } - return r + return app } diff --git a/service/cron_service/tasks.go b/service/cron_service/tasks.go index c74d899..7b9241d 100644 --- a/service/cron_service/tasks.go +++ b/service/cron_service/tasks.go @@ -36,7 +36,7 @@ func AddTask(task ScheduledTask) cron.EntryID { logging.Logger.Error(fmt.Sprintf("添加定时任务失败,原因:%s", err)) } else { TaskList[taskId] = &task - logging.Logger.Error(fmt.Sprintf("添加日志清除任务成功,entryID: %d", taskId)) + logging.Logger.Error(fmt.Sprintf("添加日志清除任务成功,entryID: %d, cron: %s", taskId, task.Schedule)) } return taskId }