Files
Message-Push-Nest/routers/router.go
T

185 lines
5.4 KiB
Go
Raw Normal View History

2023-12-30 17:40:20 +08:00
package routers
import (
2024-01-02 11:50:54 +08:00
"embed"
2026-01-15 20:05:00 +08:00
"fmt"
2023-12-30 17:40:20 +08:00
"github.com/gin-gonic/gin"
2026-01-15 20:05:00 +08:00
"io"
2024-01-02 11:50:54 +08:00
"io/fs"
2023-12-30 17:40:20 +08:00
"message-nest/middleware"
2024-01-02 11:50:54 +08:00
"message-nest/pkg/setting"
2023-12-30 17:40:20 +08:00
"message-nest/routers/api"
"message-nest/routers/api/v1"
2025-12-06 00:28:18 +08:00
"message-nest/routers/api/v2"
2024-01-02 11:50:54 +08:00
"net/http"
2026-01-15 20:05:00 +08:00
"strings"
2023-12-30 17:40:20 +08:00
)
2024-01-02 14:18:23 +08:00
// AppendCors 添加是否跨域(debug模式开启)
2024-01-02 14:45:34 +08:00
func AppendCors(app *gin.Engine) {
2024-01-02 14:18:23 +08:00
if setting.ServerSetting.RunMode == "debug" {
2024-01-02 14:45:34 +08:00
app.Use(middleware.Cors())
2024-01-02 14:18:23 +08:00
}
}
2026-01-15 20:05:00 +08:00
// AppendServerStaticHtmlWithPrefix 启用返回打包的静态文件(支持路径前缀)
func AppendServerStaticHtmlWithPrefix(router gin.IRouter, f embed.FS, pathPrefix string) {
2024-01-02 11:50:54 +08:00
if setting.ServerSetting.EmbedHtml == "disable" {
return
}
2024-01-02 14:18:23 +08:00
2024-01-02 11:50:54 +08:00
assets, _ := fs.Sub(f, "web/dist/assets")
dist, _ := fs.Sub(f, "web/dist")
2026-01-15 20:05:00 +08:00
// 根据是否有路径前缀来设置静态文件路由
if pathPrefix != "" {
// 有路径前缀时,使用相对路径
if r, ok := router.(*gin.RouterGroup); ok {
r.Use(middleware.StaticCacheMiddleware())
r.StaticFS("/assets", http.FS(assets))
r.GET("/", func(ctx *gin.Context) {
// 读取 index.html
indexFile, err := dist.Open("index.html")
if err != nil {
ctx.String(http.StatusInternalServerError, "Failed to load index.html")
return
}
defer indexFile.Close()
2024-01-02 11:50:54 +08:00
2026-01-15 20:05:00 +08:00
// 读取文件内容
content, err := io.ReadAll(indexFile)
if err != nil {
ctx.String(http.StatusInternalServerError, "Failed to read index.html")
return
}
// 注入配置脚本
configScript := fmt.Sprintf(`<script>window.__URL_PATH_PREFIX__ = '%s';</script>`, pathPrefix)
htmlContent := string(content)
// 在 </head> 标签前注入配置
htmlContent = strings.Replace(htmlContent, "</head>", configScript+"</head>", 1)
ctx.Header("Content-Type", "text/html; charset=utf-8")
ctx.String(http.StatusOK, htmlContent)
})
}
} else {
// 无路径前缀时,使用原有逻辑
if r, ok := router.(*gin.Engine); ok {
r.Use(middleware.StaticCacheMiddleware())
r.StaticFS("assets/", http.FS(assets))
r.GET("/", func(ctx *gin.Context) {
ctx.FileFromFS("/", http.FS(dist))
})
}
}
}
// AppendServerStaticHtml 启用返回打包的静态文件(保留向后兼容)
func AppendServerStaticHtml(app *gin.Engine, f embed.FS) {
AppendServerStaticHtmlWithPrefix(app, f, "")
2024-01-02 11:50:54 +08:00
}
2024-01-02 14:45:34 +08:00
// InitRouter 初始化路由
2024-01-02 11:50:54 +08:00
func InitRouter(f embed.FS) *gin.Engine {
2024-01-02 14:45:34 +08:00
app := gin.New()
app.Use(middleware.LogMiddleware())
app.Use(gin.Recovery())
2023-12-30 17:40:20 +08:00
2024-01-02 14:45:34 +08:00
AppendCors(app)
2026-01-15 20:05:00 +08:00
// 获取 URL 前缀
pathPrefix := setting.ServerSetting.UrlPrefix
if pathPrefix != "" && pathPrefix[0] != '/' {
pathPrefix = "/" + pathPrefix
}
// 如果有路径前缀,创建路由组
var router gin.IRouter
if pathPrefix != "" {
router = app.Group(pathPrefix)
} else {
router = app
}
AppendServerStaticHtmlWithPrefix(router, f, pathPrefix)
2023-12-30 17:40:20 +08:00
2026-01-15 20:05:00 +08:00
router.POST("/auth", api.GetAuth)
apiV1 := router.Group("/api/v1")
2024-01-02 14:45:34 +08:00
apiV1.Use(middleware.JWT())
2023-12-30 17:40:20 +08:00
{
// sendways
2024-01-02 14:45:34 +08:00
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)
2023-12-30 17:40:20 +08:00
// sendtasks
2024-01-02 14:45:34 +08:00
apiV1.GET("/sendtasks/list", v1.GetMsgSendTaskList)
apiV1.POST("/sendtasks/add", v1.AddMsgSendTask)
apiV1.POST("/sendtasks/delete", v1.DeleteMsgSendTask)
apiV1.POST("/sendtasks/edit", v1.EditMsgSendTask)
2024-04-11 17:20:36 +08:00
apiV1.GET("/sendtasks/get", v1.GetMsgSendTask)
2023-12-30 17:40:20 +08:00
// sendtasks/ins
2024-01-02 14:45:34 +08:00
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)
2024-01-14 17:05:35 +08:00
apiV1.POST("/sendtasks/ins/update_enable", v1.UpdateMsgTaskInsEnable)
2023-12-30 17:40:20 +08:00
// message/send
2024-01-02 14:45:34 +08:00
apiV1.POST("/message/send", v1.DoSendMassage)
2023-12-30 17:40:20 +08:00
2024-01-02 14:45:34 +08:00
apiV1.GET("/sendlogs/list", v1.GetTaskSendLogsList)
2023-12-30 17:40:20 +08:00
// settings
2024-01-02 14:45:34 +08:00
apiV1.POST("/settings/setpasswd", v1.EditPasswd)
apiV1.POST("/settings/set", v1.EditSettings)
2024-01-17 23:15:47 +08:00
apiV1.POST("/settings/reset", v1.RestDefaultSettings)
2024-01-02 14:45:34 +08:00
apiV1.GET("/settings/getsetting", v1.GetUserSetting)
2023-12-30 17:40:20 +08:00
2025-10-01 00:19:48 +08:00
// login logs
apiV1.GET("/loginlogs/recent", v1.GetRecentLoginLogs)
2024-01-24 21:33:51 +08:00
// statistic
apiV1.GET("/statistic", v1.GetStatisticData)
2026-01-06 16:20:02 +08:00
apiV1.GET("/statistic/task", v1.GetSendStatsByTask)
2024-01-24 21:33:51 +08:00
2024-04-11 17:20:36 +08:00
// cronMessage
2025-01-01 12:19:04 +08:00
apiV1.POST("/cronmessages/addone", v1.AddCronMsgTask)
apiV1.GET("/cronmessages/list", v1.GetCronMsgList)
apiV1.POST("/cronmessages/delete", v1.DeleteCronMsgTask)
apiV1.POST("/cronmessages/edit", v1.EditCronMsgTask)
2025-10-12 14:35:49 +08:00
apiV1.POST("/cronmessages/sendnow", v1.SendNowCronMsg)
2024-04-11 17:20:36 +08:00
2025-01-01 17:30:02 +08:00
// hostedMessage
apiV1.GET("/hostedmessages/list", v1.GetHostMessageList)
2025-12-06 00:28:18 +08:00
// messageTemplate
apiV1.GET("/templates/list", v1.GetMessageTemplateList)
apiV1.GET("/templates/get", v1.GetMessageTemplate)
apiV1.POST("/templates/add", v1.AddMessageTemplate)
apiV1.POST("/templates/edit", v1.EditMessageTemplate)
apiV1.POST("/templates/delete", v1.DeleteMessageTemplate)
apiV1.POST("/templates/preview", v1.PreviewMessageTemplate)
// messageTemplate instances
apiV1.GET("/templates/ins/get", v1.GetTemplateWithIns)
apiV1.POST("/templates/ins/addone", v1.AddTemplateIns)
}
// API v2
2026-01-15 20:05:00 +08:00
apiV2 := router.Group("/api/v2")
2025-12-06 00:28:18 +08:00
apiV2.Use(middleware.JWT())
{
// message/send - 使用模板发送消息
apiV2.POST("/message/send", v2.DoSendMessageByTemplate)
2023-12-30 17:40:20 +08:00
}
2024-01-02 14:45:34 +08:00
return app
2023-12-30 17:40:20 +08:00
}