123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package initialize
- import (
- "confrontation-training/api/chat"
- "confrontation-training/api/gateway"
- _ "confrontation-training/docs"
- "confrontation-training/global"
- _ "confrontation-training/global"
- "confrontation-training/middleware"
- "confrontation-training/models"
- modelsChat "confrontation-training/models/chat"
- modelsDevice "confrontation-training/models/gateway"
- "fmt"
- "github.com/gin-gonic/gin"
- log "github.com/sirupsen/logrus"
- swaggerFiles "github.com/swaggo/files"
- ginSwagger "github.com/swaggo/gin-swagger"
- "net/http"
- )
- func Router() {
- //初始化表格
- initTableErr := global.Db.AutoMigrate(&models.User{}, &modelsChat.Question{}, &modelsChat.Answer{}, &modelsDevice.DeviceInfo{})
- if initTableErr != nil {
- fmt.Printf("初始化表格异常:%s", initTableErr.Error())
- log.Infof("初始化表格异常:%s", initTableErr.Error())
- return
- }
- gin.SetMode(gin.ReleaseMode)
- //gin.SetMode(gin.DebugMode)
- engine := gin.Default()
- //解决跨域
- engine.Use(middleware.Cors())
- engine.Use(middleware.LoggerToFile())
- // 静态资源请求映射
- engine.Static("/image", global.Config.Upload.SavePath)
- //404 NOT FOUND
- engine.NoRoute(func(context *gin.Context) {
- context.String(http.StatusNotFound, "404 not found")
- })
- //swag
- engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
- engine.Static("/file", "./files")
- engine.GET("/ws", WsHandler)
- //group
- v1 := engine.Group("/v1")
- user := v1.Group("/user")
- user.POST("/login", gateway.GetUser().UserLogin)
- //user.Use(middleware.JwtAuth())
- user.POST("/register", gateway.GetUser().UseRegister)
- user.POST("/reset", gateway.GetUser().ResetPassword)
- user.GET("/find", gateway.GetUser().UserList)
- user.POST("/change/password", gateway.GetUser().ModePass)
- device := v1.Group("/device")
- device.POST("/scan", gateway.ScanDevice)
- device.POST("/connection", gateway.ConnectDevice)
- device.POST("/write/data/", gateway.WriteData)
- device.GET("/open/notify/", gateway.OpenNotify)
- device.GET("/:mac/stop/trans/", gateway.StopTrans)
- device.GET("/:mac/stop/collect/", gateway.StopCollect)
- device.GET("/:mac/disconnect", gateway.GetDeviceService().Disconnect)
- device.POST("/add/", gateway.GetDeviceService().DeviceAdd)
- device.DELETE("/:mac/remove/", gateway.GetDeviceService().DeviceRemove)
- device.GET("/list/", gateway.GetDeviceService().DeviceList)
- device.GET("connected", gateway.GetDeviceService().DeviceConnected)
- chats := v1.Group("/chat")
- chats.GET("/get/chat/", chat.GetQuestionService().GetQuestion)
- chats.POST("/create/chat/", chat.GetQuestionService().CreateQuestion)
- chats.GET("/get/answer/", chat.GetAnswerService().GetAnswer)
- v2 := engine.Group("/v2")
- device2 := v2.Group("/device")
- device2.GET("/scan", gateway.ScanDeviceEmq)
- device2.GET("/stop/scan", gateway.StopScanDeviceEmq)
- device2.POST("/conn", gateway.ConnectDevice2)
- device2.POST("/dis/conn", gateway.DisConnect)
- device2.GET("/dis/connAll", gateway.DisConnectAll)
- device2.GET("/connected/list", gateway.ConnectList)
- device2.POST("/write/data", gateway.WriteDataEmq)
- gate := v2.Group("/gateway")
- gate.GET("/find", gateway.FindGateway)
- gate.POST("/set/", gateway.SetGatewayMac)
- //初始化SSE
- //
- //record := v1.Group("/record")
- //record.POST("/create", api.GetRecord().SaveRecord)
- //record.POST("/find", api.GetRecord().RecordList)
- //record.GET("/:recordId/find", api.GetRecord().GetRecordById)
- err := engine.Run(fmt.Sprintf(":%s", global.Config.Server.Port))
- if err != nil {
- fmt.Printf("%s", err.Error())
- log.Infof("%s", err.Error())
- return
- }
- }
|