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

84 lines
2.1 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"
2023-12-30 17:40:20 +08:00
"github.com/gin-gonic/gin"
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"
2024-01-02 11:50:54 +08:00
"net/http"
2023-12-30 17:40:20 +08:00
)
2024-01-02 14:18:23 +08:00
// AppendCors 添加是否跨域(debug模式开启)
func AppendCors(r *gin.Engine) {
if setting.ServerSetting.RunMode == "debug" {
r.Use(middleware.Cors())
}
}
// ServerStaticHtml 启用返回打包的静态文件
func AppendServerStaticHtml(r *gin.Engine, f embed.FS) {
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")
r.StaticFS("assets/", http.FS(assets))
r.GET("/", func(ctx *gin.Context) {
ctx.FileFromFS("/", http.FS(dist))
})
}
2023-12-30 17:40:20 +08:00
// InitRouter initialize routing information
2024-01-02 11:50:54 +08:00
func InitRouter(f embed.FS) *gin.Engine {
2023-12-30 17:40:20 +08:00
r := gin.New()
2024-01-01 12:03:56 +08:00
r.Use(middleware.LogMiddleware())
2023-12-30 17:40:20 +08:00
r.Use(gin.Recovery())
2024-01-02 14:18:23 +08:00
AppendCors(r)
AppendServerStaticHtml(r, f)
2023-12-30 17:40:20 +08:00
2024-01-02 11:50:54 +08:00
r.POST("/auth", api.GetAuth)
2023-12-30 17:40:20 +08:00
apiv1 := r.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)
// sendtasks
apiv1.GET("/sendtasks/list", v1.GetMsgSendTaskList)
apiv1.POST("/sendtasks/add", v1.AddMsgSendTask)
apiv1.POST("/sendtasks/delete", v1.DeleteMsgSendTask)
2024-01-01 12:03:56 +08:00
apiv1.POST("/sendtasks/edit", v1.EditMsgSendTask)
2023-12-30 17:40:20 +08:00
// 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)
// message/send
apiv1.POST("/message/send", v1.DoSendMassage)
apiv1.GET("/sendlogs/list", v1.GetTaskSendLogsList)
// settings
apiv1.POST("/settings/setpasswd", v1.EditPasswd)
2023-12-31 21:31:56 +08:00
apiv1.POST("/settings/set", v1.EditSettings)
apiv1.GET("/settings/getsetting", v1.GetUserSetting)
2023-12-30 17:40:20 +08:00
}
return r
}