package router import ( "fmt" "git.hpds.cc/Component/logging" ginzap "github.com/gin-contrib/zap" "github.com/gin-gonic/gin" "hpds-iot-web/config" "hpds-iot-web/internal/handler" "hpds-iot-web/internal/middleware" "hpds-iot-web/mq" "net/http" "xorm.io/xorm" m "hpds-iot-web/model" e "hpds-iot-web/pkg/err" ) func InitRouter(cfg *config.WebConfig, logger *logging.Logger, engine *xorm.Engine) *gin.Engine { hs := handler.NewHandlerService(cfg, engine, logger) gin.SetMode(gin.ReleaseMode) root := gin.New() root.Use(ginzap.Ginzap(logger.Logger, "2006-01-02 15:04:05.000", true)) root.Use(middleware.Cors()) r := root.Group("/api") { user := r.Group("/user") { user.Use(middleware.JwtAuthMiddleware(logger.Logger)) user.POST("/login", e.ErrorWrapper(hs.Login)) user.POST("/add", e.ErrorWrapper(hs.AddUser)) user.POST("/edit", e.ErrorWrapper(hs.EditUser)) user.POST("/list", e.ErrorWrapper(hs.GetUserList)) user.GET("/getUserInfo", e.ErrorWrapper(hs.GetUserInfo)) menu := user.Group("/menu") { menu.GET("/list", e.ErrorWrapper(hs.MenuList)) } } 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)) } manage := r.Group("/manage") { manage.Use(middleware.JwtAuthMiddleware(logger.Logger)) owner := manage.Group("/owner") { owner.POST("/list", e.ErrorWrapper(hs.OwnerList)) owner.GET("/info", e.ErrorWrapper(hs.OwnerInfo)) owner.POST("/add", e.ErrorWrapper(hs.AddOwner)) owner.POST("/edit", e.ErrorWrapper(hs.EditOwner)) owner.POST("/delete", e.ErrorWrapper(hs.DelOwner)) } project := manage.Group("/project") { project.POST("/list", e.ErrorWrapper(hs.ProjectList)) project.POST("/info", e.ErrorWrapper(hs.ProjectInfo)) project.POST("/add", e.ErrorWrapper(hs.AddProject)) project.POST("/edit", e.ErrorWrapper(hs.EditProject)) project.POST("/delete", e.ErrorWrapper(hs.DelProject)) project.POST("/result", e.ErrorWrapper(hs.GetProjectResult)) project.POST("/data", e.ErrorWrapper(hs.GetProjectData)) } product := manage.Group("/product") { product.POST("/list", e.ErrorWrapper(hs.ProductList)) product.POST("/info", e.ErrorWrapper(hs.GetProductInfo)) product.POST("/add", e.ErrorWrapper(hs.AddProduct)) product.POST("/edit", e.ErrorWrapper(hs.EditProduct)) product.POST("/delete", e.ErrorWrapper(hs.DelProduct)) 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") { 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)) } 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)) 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)) } } service := product.Group("/service") { 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)) } } 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)) } } //detection := manage.Group("/detection") //{ // //} } model := r.Group("/model") { model.Use(middleware.JwtAuthMiddleware(logger.Logger)) 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)) model.POST("/issue", e.ErrorWrapper(hs.ModelIssue)) model.POST("/issueLog", e.ErrorWrapper(hs.ModelIssueLog)) model.POST("/workflow", e.ErrorWrapper(hs.GetModelWorkflow)) } file := r.Group("/file") { file.Use(middleware.JwtAuthMiddleware(logger.Logger)) file.POST("/upload", e.ErrorWrapper(hs.UploadFile)) file.POST("/list", e.ErrorWrapper(hs.FileList)) file.POST("/label", e.ErrorWrapper(hs.FileLabel)) //file.POST("/batchLabel", e.ErrorWrapper(hs.FileBatchLabel)) } 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)) } 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)) 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") us, _ := c.Get("operatorUser") if us == nil { return } userInfo := us.(*m.SystemUser) mq.AddChannel(fmt.Sprintf("%d", userInfo.UserId)) w := c.Writer //flusher, _ := w.(http.Flusher) closeNotify := c.Request.Context().Done() go func() { <-closeNotify delete(mq.ChannelsMap, fmt.Sprintf("%d", userInfo.UserId)) logger.Info("SSE close for user = " + fmt.Sprintf("%d", userInfo.UserId)) return }() // 循环监听 SSE 事件通道 for { // 从通道中读取事件 message := <-mq.ChannelsMap[fmt.Sprintf("%d", userInfo.UserId)] // 向客户端发送 SSE 事件 _, _ = fmt.Fprintf(w, "%s", &message) // 刷新 response buffer w.(http.Flusher).Flush() } //_, _ = 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() //} }) train := task.Group("/train") { train.POST("/create", e.ErrorWrapper(hs.CreateTrainingTask)) train.POST("/edit", e.ErrorWrapper(hs.EditTrainingTask)) train.POST("/reRun", e.ErrorWrapper(hs.ReRunTrainingTask)) train.POST("/list", e.ErrorWrapper(hs.TrainingTaskList)) train.POST("/info", e.ErrorWrapper(hs.TrainingTaskInfo)) train.POST("/result", e.ErrorWrapper(hs.TrainingTaskResult)) train.POST("/log", e.ErrorWrapper(hs.TrainingTaskLog)) } } disease := r.Group("/disease") { disease.Use(middleware.JwtAuthMiddleware(logger.Logger)) disease.POST("/list", e.ErrorWrapper(hs.DiseaseList)) disease.POST("/statistics", e.ErrorWrapper(hs.DiseaseStatistics)) disease.POST("/create", e.ErrorWrapper(hs.CreateTrainDatasetByDisease)) 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)) } } 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)) dataset.POST("/info", e.ErrorWrapper(hs.DatasetInfo)) } training := r.Group("/trainDataset") { training.Use(middleware.JwtAuthMiddleware(logger.Logger)) training.POST("/create", e.ErrorWrapper(hs.CreateTrainDataset)) training.POST("/list", e.ErrorWrapper(hs.TrainDatasetList)) training.POST("/fileList", e.ErrorWrapper(hs.TrainDatasetFileList)) //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)) } 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)) } edge := r.Group("/edge") { edge.Use(middleware.JwtAuthMiddleware(logger.Logger)) dir := edge.Group("/directory") { dir.POST("/list", e.ErrorWrapper(hs.GetEdgeList)) dir.POST("/info", e.ErrorWrapper(hs.GetEdgeInfo)) } } analysis := r.Group("/analysis") { analysis.Use(middleware.JwtAuthMiddleware(logger.Logger)) analysis.POST("/info", e.ErrorWrapper(hs.GetAnalysisInfo)) } } return root }