router.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package initialize
  2. import (
  3. "confrontation-training/api/chat"
  4. "confrontation-training/api/gateway"
  5. _ "confrontation-training/docs"
  6. "confrontation-training/global"
  7. _ "confrontation-training/global"
  8. "confrontation-training/middleware"
  9. "confrontation-training/models"
  10. modelsChat "confrontation-training/models/chat"
  11. modelsDevice "confrontation-training/models/gateway"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. log "github.com/sirupsen/logrus"
  15. swaggerFiles "github.com/swaggo/files"
  16. ginSwagger "github.com/swaggo/gin-swagger"
  17. "net/http"
  18. )
  19. func Router() {
  20. //初始化表格
  21. initTableErr := global.Db.AutoMigrate(&models.User{}, &modelsChat.Question{}, &modelsChat.Answer{}, &modelsDevice.DeviceInfo{})
  22. if initTableErr != nil {
  23. fmt.Printf("初始化表格异常:%s", initTableErr.Error())
  24. log.Infof("初始化表格异常:%s", initTableErr.Error())
  25. return
  26. }
  27. gin.SetMode(gin.ReleaseMode)
  28. //gin.SetMode(gin.DebugMode)
  29. engine := gin.Default()
  30. //解决跨域
  31. engine.Use(middleware.Cors())
  32. engine.Use(middleware.LoggerToFile())
  33. // 静态资源请求映射
  34. engine.Static("/image", global.Config.Upload.SavePath)
  35. //404 NOT FOUND
  36. engine.NoRoute(func(context *gin.Context) {
  37. context.String(http.StatusNotFound, "404 not found")
  38. })
  39. //swag
  40. engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  41. engine.Static("/file", "./files")
  42. engine.GET("/ws", WsHandler)
  43. //group
  44. v1 := engine.Group("/v1")
  45. user := v1.Group("/user")
  46. user.POST("/login", gateway.GetUser().UserLogin)
  47. //user.Use(middleware.JwtAuth())
  48. user.POST("/register", gateway.GetUser().UseRegister)
  49. user.POST("/reset", gateway.GetUser().ResetPassword)
  50. user.GET("/find", gateway.GetUser().UserList)
  51. user.POST("/change/password", gateway.GetUser().ModePass)
  52. device := v1.Group("/device")
  53. device.POST("/scan", gateway.ScanDevice)
  54. device.POST("/connection", gateway.ConnectDevice)
  55. device.POST("/write/data/", gateway.WriteData)
  56. device.GET("/open/notify/", gateway.OpenNotify)
  57. device.GET("/:mac/stop/trans/", gateway.StopTrans)
  58. device.GET("/:mac/stop/collect/", gateway.StopCollect)
  59. device.GET("/:mac/disconnect", gateway.GetDeviceService().Disconnect)
  60. device.POST("/add/", gateway.GetDeviceService().DeviceAdd)
  61. device.DELETE("/:mac/remove/", gateway.GetDeviceService().DeviceRemove)
  62. device.GET("/list/", gateway.GetDeviceService().DeviceList)
  63. device.GET("connected", gateway.GetDeviceService().DeviceConnected)
  64. chats := v1.Group("/chat")
  65. chats.GET("/get/chat/", chat.GetQuestionService().GetQuestion)
  66. chats.POST("/create/chat/", chat.GetQuestionService().CreateQuestion)
  67. chats.GET("/get/answer/", chat.GetAnswerService().GetAnswer)
  68. v2 := engine.Group("/v2")
  69. device2 := v2.Group("/device")
  70. device2.GET("/scan", gateway.ScanDeviceEmq)
  71. device2.GET("/stop/scan", gateway.StopScanDeviceEmq)
  72. device2.POST("/conn", gateway.ConnectDevice2)
  73. device2.POST("/dis/conn", gateway.DisConnect)
  74. device2.GET("/dis/connAll", gateway.DisConnectAll)
  75. device2.GET("/connected/list", gateway.ConnectList)
  76. device2.POST("/write/data", gateway.WriteDataEmq)
  77. gate := v2.Group("/gateway")
  78. gate.GET("/find", gateway.FindGateway)
  79. gate.POST("/set/", gateway.SetGatewayMac)
  80. //初始化SSE
  81. //
  82. //record := v1.Group("/record")
  83. //record.POST("/create", api.GetRecord().SaveRecord)
  84. //record.POST("/find", api.GetRecord().RecordList)
  85. //record.GET("/:recordId/find", api.GetRecord().GetRecordById)
  86. err := engine.Run(fmt.Sprintf(":%s", global.Config.Server.Port))
  87. if err != nil {
  88. fmt.Printf("%s", err.Error())
  89. log.Infof("%s", err.Error())
  90. return
  91. }
  92. }