2023-01-06 10:09:23 +08:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2023-04-24 15:21:17 +08:00
|
|
|
"fmt"
|
2023-01-06 19:15:52 +08:00
|
|
|
"git.hpds.cc/Component/logging"
|
2023-01-06 10:09:23 +08:00
|
|
|
ginzap "github.com/gin-contrib/zap"
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-01-11 18:05:29 +08:00
|
|
|
"hpds-iot-web/config"
|
2023-01-06 10:09:23 +08:00
|
|
|
"hpds-iot-web/internal/handler"
|
|
|
|
"hpds-iot-web/internal/middleware"
|
2023-04-24 15:21:17 +08:00
|
|
|
"hpds-iot-web/mq"
|
|
|
|
"net/http"
|
2023-01-06 10:09:23 +08:00
|
|
|
"xorm.io/xorm"
|
|
|
|
|
2023-05-14 18:23:12 +08:00
|
|
|
m "hpds-iot-web/model"
|
2023-01-06 10:09:23 +08:00
|
|
|
e "hpds-iot-web/pkg/err"
|
|
|
|
)
|
|
|
|
|
2023-01-11 18:05:29 +08:00
|
|
|
func InitRouter(cfg *config.WebConfig, logger *logging.Logger, engine *xorm.Engine) *gin.Engine {
|
|
|
|
hs := handler.NewHandlerService(cfg, engine, logger)
|
2023-01-06 10:09:23 +08:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
2023-01-06 17:48:53 +08:00
|
|
|
root := gin.New()
|
2023-01-06 19:15:52 +08:00
|
|
|
root.Use(ginzap.Ginzap(logger.Logger, "2006-01-02 15:04:05.000", true))
|
2023-01-06 17:48:53 +08:00
|
|
|
root.Use(middleware.Cors())
|
|
|
|
r := root.Group("/api")
|
2023-01-06 10:09:23 +08:00
|
|
|
{
|
2023-01-06 17:48:53 +08:00
|
|
|
user := r.Group("/user")
|
2023-01-06 10:09:23 +08:00
|
|
|
{
|
2023-01-06 19:15:52 +08:00
|
|
|
user.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
2023-01-06 17:48:53 +08:00
|
|
|
user.POST("/login", e.ErrorWrapper(hs.Login))
|
2023-04-02 23:36:16 +08:00
|
|
|
user.POST("/add", e.ErrorWrapper(hs.AddUser))
|
|
|
|
user.POST("/edit", e.ErrorWrapper(hs.EditUser))
|
2023-03-23 18:03:09 +08:00
|
|
|
user.POST("/list", e.ErrorWrapper(hs.GetUserList))
|
2023-01-06 17:48:53 +08:00
|
|
|
user.GET("/getUserInfo", e.ErrorWrapper(hs.GetUserInfo))
|
2023-01-10 10:01:42 +08:00
|
|
|
|
|
|
|
menu := user.Group("/menu")
|
|
|
|
{
|
|
|
|
menu.GET("/list", e.ErrorWrapper(hs.MenuList))
|
|
|
|
}
|
2023-01-06 10:09:23 +08:00
|
|
|
}
|
2023-05-14 18:23:12 +08:00
|
|
|
role := r.Group("/role")
|
|
|
|
{
|
|
|
|
role.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
|
|
|
role.POST("/list", e.ErrorWrapper(hs.GetRoleList))
|
|
|
|
role.POST("/edit", e.ErrorWrapper(hs.EditRole))
|
|
|
|
role.POST("/add", e.ErrorWrapper(hs.AddRole))
|
|
|
|
}
|
2023-01-06 17:48:53 +08:00
|
|
|
manage := r.Group("/manage")
|
2023-01-06 10:09:23 +08:00
|
|
|
{
|
2023-01-06 19:15:52 +08:00
|
|
|
manage.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
2023-01-06 17:48:53 +08:00
|
|
|
owner := manage.Group("/owner")
|
2023-01-06 10:09:23 +08:00
|
|
|
{
|
2023-01-06 17:48:53 +08:00
|
|
|
owner.POST("/list", e.ErrorWrapper(hs.OwnerList))
|
2023-01-10 10:01:42 +08:00
|
|
|
owner.GET("/info", e.ErrorWrapper(hs.OwnerInfo))
|
2023-01-06 17:48:53 +08:00
|
|
|
owner.POST("/add", e.ErrorWrapper(hs.AddOwner))
|
|
|
|
owner.POST("/edit", e.ErrorWrapper(hs.EditOwner))
|
|
|
|
owner.POST("/delete", e.ErrorWrapper(hs.DelOwner))
|
2023-01-06 10:09:23 +08:00
|
|
|
}
|
2023-01-06 17:48:53 +08:00
|
|
|
project := manage.Group("/project")
|
2023-01-06 10:09:23 +08:00
|
|
|
{
|
2023-01-06 17:48:53 +08:00
|
|
|
project.POST("/list", e.ErrorWrapper(hs.ProjectList))
|
2023-04-24 15:21:17 +08:00
|
|
|
project.POST("/info", e.ErrorWrapper(hs.ProjectInfo))
|
2023-01-06 17:48:53 +08:00
|
|
|
project.POST("/add", e.ErrorWrapper(hs.AddProject))
|
|
|
|
project.POST("/edit", e.ErrorWrapper(hs.EditProject))
|
|
|
|
project.POST("/delete", e.ErrorWrapper(hs.DelProject))
|
|
|
|
|
2023-01-06 10:09:23 +08:00
|
|
|
}
|
2023-01-06 17:48:53 +08:00
|
|
|
product := manage.Group("/product")
|
2023-01-06 10:09:23 +08:00
|
|
|
{
|
2023-01-06 17:48:53 +08:00
|
|
|
product.POST("/list", e.ErrorWrapper(hs.ProductList))
|
2023-01-10 10:01:42 +08:00
|
|
|
|
|
|
|
product.POST("/info", e.ErrorWrapper(hs.GetProductInfo))
|
2023-01-06 17:48:53 +08:00
|
|
|
product.POST("/add", e.ErrorWrapper(hs.AddProduct))
|
|
|
|
product.POST("/edit", e.ErrorWrapper(hs.EditProduct))
|
|
|
|
product.POST("/delete", e.ErrorWrapper(hs.DelProduct))
|
2023-01-06 14:38:22 +08:00
|
|
|
|
2023-01-06 17:48:53 +08:00
|
|
|
category := product.Group("/category")
|
|
|
|
{
|
|
|
|
category.POST("/list", e.ErrorWrapper(hs.CategoryList))
|
|
|
|
category.POST("/add", e.ErrorWrapper(hs.AddCategory))
|
|
|
|
category.POST("/edit", e.ErrorWrapper(hs.EditCategory))
|
|
|
|
category.POST("/delete", e.ErrorWrapper(hs.DelCategory))
|
|
|
|
}
|
|
|
|
attribute := product.Group("/attribute")
|
2023-01-06 14:38:22 +08:00
|
|
|
{
|
2023-01-06 17:48:53 +08:00
|
|
|
attribute.POST("/list", e.ErrorWrapper(hs.AttributeList))
|
|
|
|
attribute.POST("/add", e.ErrorWrapper(hs.AddAttribute))
|
|
|
|
attribute.POST("/edit", e.ErrorWrapper(hs.EditAttribute))
|
|
|
|
attribute.POST("/delete", e.ErrorWrapper(hs.DelAttribute))
|
2023-01-06 14:38:22 +08:00
|
|
|
}
|
2023-01-06 17:48:53 +08:00
|
|
|
event := product.Group("/event")
|
|
|
|
{
|
|
|
|
event.POST("/list", e.ErrorWrapper(hs.EventList))
|
|
|
|
event.POST("/add", e.ErrorWrapper(hs.AddEvent))
|
|
|
|
event.POST("/edit", e.ErrorWrapper(hs.EditEvent))
|
|
|
|
event.POST("/delete", e.ErrorWrapper(hs.DelEvent))
|
2023-01-06 14:38:22 +08:00
|
|
|
|
2023-01-06 17:48:53 +08:00
|
|
|
params := event.Group("/params")
|
|
|
|
{
|
|
|
|
params.POST("/list", e.ErrorWrapper(hs.EventParamsList))
|
|
|
|
params.POST("/add", e.ErrorWrapper(hs.AddEventParams))
|
|
|
|
params.POST("/edit", e.ErrorWrapper(hs.EditEventParams))
|
|
|
|
params.POST("/delete", e.ErrorWrapper(hs.DelEventParams))
|
|
|
|
}
|
2023-01-06 14:38:22 +08:00
|
|
|
|
2023-01-06 17:48:53 +08:00
|
|
|
}
|
|
|
|
service := product.Group("/service")
|
2023-01-06 14:38:22 +08:00
|
|
|
{
|
2023-01-06 17:48:53 +08:00
|
|
|
service.POST("/list", e.ErrorWrapper(hs.ServiceList))
|
|
|
|
service.POST("/add", e.ErrorWrapper(hs.AddService))
|
|
|
|
service.POST("/edit", e.ErrorWrapper(hs.EditService))
|
|
|
|
service.POST("/delete", e.ErrorWrapper(hs.DelService))
|
|
|
|
|
|
|
|
params := service.Group("/params")
|
|
|
|
{
|
|
|
|
params.POST("/list", e.ErrorWrapper(hs.ServiceParamsList))
|
|
|
|
params.POST("/add", e.ErrorWrapper(hs.AddServiceParams))
|
|
|
|
params.POST("/edit", e.ErrorWrapper(hs.EditServiceParams))
|
|
|
|
params.POST("/delete", e.ErrorWrapper(hs.DelServiceParams))
|
|
|
|
}
|
2023-01-06 14:38:22 +08:00
|
|
|
}
|
2023-01-11 18:05:29 +08:00
|
|
|
device := product.Group("/device")
|
|
|
|
{
|
|
|
|
device.POST("/list", e.ErrorWrapper(hs.DeviceList))
|
|
|
|
device.POST("/info", e.ErrorWrapper(hs.DeviceInfo))
|
|
|
|
device.POST("/add", e.ErrorWrapper(hs.AddDevice))
|
|
|
|
device.POST("/edit", e.ErrorWrapper(hs.EditDevice))
|
|
|
|
device.POST("/delete", e.ErrorWrapper(hs.DeleteDevice))
|
|
|
|
}
|
2023-01-06 10:09:23 +08:00
|
|
|
}
|
2023-03-23 18:03:09 +08:00
|
|
|
//detection := manage.Group("/detection")
|
|
|
|
//{
|
|
|
|
//
|
|
|
|
//}
|
2023-01-06 10:09:23 +08:00
|
|
|
}
|
2023-01-06 17:48:53 +08:00
|
|
|
model := r.Group("/model")
|
|
|
|
{
|
2023-01-06 19:15:52 +08:00
|
|
|
model.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
2023-01-06 17:48:53 +08:00
|
|
|
model.POST("/list", e.ErrorWrapper(hs.ModelList))
|
|
|
|
model.POST("/add", e.ErrorWrapper(hs.AddModel))
|
|
|
|
model.POST("/edit", e.ErrorWrapper(hs.EditModel))
|
|
|
|
model.POST("/delete", e.ErrorWrapper(hs.DelModel))
|
2023-03-23 18:03:09 +08:00
|
|
|
model.POST("/issue", e.ErrorWrapper(hs.ModelIssue))
|
|
|
|
model.POST("/workflow", e.ErrorWrapper(hs.GetModelWorkflow))
|
2023-01-06 17:48:53 +08:00
|
|
|
}
|
2023-01-11 18:05:29 +08:00
|
|
|
file := r.Group("/file")
|
|
|
|
{
|
|
|
|
file.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
|
|
|
file.POST("/upload", e.ErrorWrapper(hs.UploadFile))
|
2023-04-24 15:21:17 +08:00
|
|
|
file.POST("/list", e.ErrorWrapper(hs.FileList))
|
2023-01-11 18:05:29 +08:00
|
|
|
}
|
|
|
|
system := r.Group("/system")
|
|
|
|
{
|
|
|
|
system.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
|
|
|
brand := system.Group("/brand")
|
|
|
|
{
|
|
|
|
brand.POST("/list", e.ErrorWrapper(hs.BrandList))
|
|
|
|
brand.POST("/info", e.ErrorWrapper(hs.BrandInfo))
|
|
|
|
brand.POST("/add", e.ErrorWrapper(hs.AddBrand))
|
|
|
|
brand.POST("/edit", e.ErrorWrapper(hs.EditBrand))
|
|
|
|
brand.POST("/delete", e.ErrorWrapper(hs.DeleteBrand))
|
|
|
|
}
|
2023-03-23 18:03:09 +08:00
|
|
|
node := system.Group("/node")
|
|
|
|
{
|
|
|
|
node.POST("/list", e.ErrorWrapper(hs.NodeList))
|
|
|
|
node.POST("/info", e.ErrorWrapper(hs.NodeInfo))
|
|
|
|
node.POST("/edit", e.ErrorWrapper(hs.EditNode))
|
|
|
|
|
|
|
|
node.POST("/state", e.ErrorWrapper(hs.NodeState))
|
|
|
|
node.POST("/last", e.ErrorWrapper(hs.NodeLastState))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
task := r.Group("/task")
|
|
|
|
{
|
|
|
|
task.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
|
|
|
task.POST("/list", e.ErrorWrapper(hs.TaskList))
|
|
|
|
task.POST("/add", e.ErrorWrapper(hs.AddTask))
|
2023-04-24 15:21:17 +08:00
|
|
|
task.POST("/reRun", e.ErrorWrapper(hs.ReRunTask))
|
|
|
|
task.POST("/info", e.ErrorWrapper(hs.TaskInfo))
|
|
|
|
task.POST("/result", e.ErrorWrapper(hs.TaskResult))
|
|
|
|
task.POST("/log", e.ErrorWrapper(hs.TaskLog))
|
|
|
|
task.GET("/event", func(c *gin.Context) {
|
|
|
|
//us, _ := c.Get("operatorUser")
|
|
|
|
//userInfo := us.(*m.SystemUser)
|
|
|
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
|
|
|
c.Writer.Header().Set("Connection", "keep-alive")
|
2023-05-14 18:23:12 +08:00
|
|
|
us, _ := c.Get("operatorUser")
|
|
|
|
if us == nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userInfo := us.(*m.SystemUser)
|
|
|
|
mq.AddChannel(fmt.Sprintf("%d", userInfo.UserId))
|
2023-04-24 15:21:17 +08:00
|
|
|
w := c.Writer
|
2023-05-14 18:23:12 +08:00
|
|
|
//flusher, _ := w.(http.Flusher)
|
2023-04-24 15:21:17 +08:00
|
|
|
closeNotify := c.Request.Context().Done()
|
|
|
|
go func() {
|
|
|
|
<-closeNotify
|
2023-05-14 18:23:12 +08:00
|
|
|
delete(mq.ChannelsMap, fmt.Sprintf("%d", userInfo.UserId))
|
|
|
|
logger.Info("SSE close for user = " + fmt.Sprintf("%d", userInfo.UserId))
|
2023-04-24 15:21:17 +08:00
|
|
|
return
|
|
|
|
}()
|
2023-05-14 18:23:12 +08:00
|
|
|
// 循环监听 SSE 事件通道
|
|
|
|
for {
|
|
|
|
// 从通道中读取事件
|
|
|
|
message := <-mq.ChannelsMap[fmt.Sprintf("%d", userInfo.UserId)]
|
|
|
|
|
|
|
|
// 向客户端发送 SSE 事件
|
|
|
|
_, _ = fmt.Fprintf(w, "%s", &message)
|
|
|
|
|
|
|
|
// 刷新 response buffer
|
|
|
|
w.(http.Flusher).Flush()
|
2023-04-24 15:21:17 +08:00
|
|
|
}
|
2023-05-14 18:23:12 +08:00
|
|
|
//_, _ = fmt.Fprintf(w, "data: %s\n\n", "--ping--")
|
|
|
|
//flusher.Flush()
|
|
|
|
//for msg := range mq.ChannelsMap[fmt.Sprintf("%d", userInfo.UserId)] {
|
|
|
|
// _, _ = fmt.Fprintf(w, "data: %s\n\n", msg)
|
|
|
|
// flusher.Flush()
|
|
|
|
//}
|
2023-04-24 15:21:17 +08:00
|
|
|
})
|
2023-01-11 18:05:29 +08:00
|
|
|
}
|
2023-01-12 10:21:40 +08:00
|
|
|
disease := r.Group("/disease")
|
|
|
|
{
|
2023-01-13 11:26:39 +08:00
|
|
|
disease.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
2023-03-23 18:03:09 +08:00
|
|
|
disease.POST("/list", e.ErrorWrapper(hs.DiseaseList))
|
2023-01-12 10:21:40 +08:00
|
|
|
diseaseType := disease.Group("/type")
|
|
|
|
{
|
|
|
|
diseaseType.POST("/list", e.ErrorWrapper(hs.DiseaseTypeList))
|
|
|
|
diseaseType.POST("/add", e.ErrorWrapper(hs.AddDiseaseType))
|
|
|
|
diseaseType.POST("/edit", e.ErrorWrapper(hs.EditDiseaseType))
|
|
|
|
diseaseType.POST("/delete", e.ErrorWrapper(hs.DeleteDiseaseType))
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 11:26:39 +08:00
|
|
|
dataset := r.Group("/dataset")
|
|
|
|
{
|
|
|
|
dataset.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
|
|
|
dataset.POST("/ownerList", e.ErrorWrapper(hs.GetOwnerProjectList))
|
|
|
|
|
|
|
|
dataset.POST("/list", e.ErrorWrapper(hs.DatasetList))
|
|
|
|
dataset.POST("/import", e.ErrorWrapper(hs.ImportDataset))
|
2023-04-24 15:21:17 +08:00
|
|
|
dataset.POST("/info", e.ErrorWrapper(hs.DatasetInfo))
|
|
|
|
|
|
|
|
}
|
2023-05-14 18:23:12 +08:00
|
|
|
training := r.Group("/training")
|
|
|
|
{
|
|
|
|
training.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
|
|
|
training.POST("/create", e.ErrorWrapper(hs.CreateTraining))
|
|
|
|
//training.POST("/list", e.ErrorWrapper(hs.TrainingList))
|
|
|
|
//training.POST("/info", e.ErrorWrapper(hs.TrainingInfo))
|
|
|
|
}
|
|
|
|
label := r.Group("/label")
|
|
|
|
{
|
|
|
|
label.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
|
|
|
//label.POST("/info", e.ErrorWrapper(hs.LabelInfo))
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:21:17 +08:00
|
|
|
report := r.Group("/report")
|
|
|
|
{
|
|
|
|
report.Use(middleware.JwtAuthMiddleware(logger.Logger))
|
|
|
|
report.POST("/list", e.ErrorWrapper(hs.ReportList))
|
|
|
|
report.POST("/generate", e.ErrorWrapper(hs.GenerateReport))
|
|
|
|
//report.POST("/view", e.ErrorWrapper(hs.ViewReport))
|
2023-01-13 11:26:39 +08:00
|
|
|
}
|
2023-01-06 10:09:23 +08:00
|
|
|
}
|
2023-01-06 17:48:53 +08:00
|
|
|
return root
|
2023-01-06 10:09:23 +08:00
|
|
|
}
|