package middleware import ( "net/http" "github.com/gin-gonic/gin" "github.com/yuguorong/go/log" ) type APP_INFO struct { AppName string AppID string AppSecret string } var AppInfo = &APP_INFO{ AppName: "PowerMeter", AppID: "6L1MQezb", AppSecret: "90ebe5ecbe350923cd00a140ab88739ed2858009", } var msgInd chan interface{} = nil var router *gin.Engine func RestServerStart(mainloop chan interface{}) { // Engin msgInd = mainloop router = gin.Default() router.GET("/Gateway4G/UpdateDevcie", UpdateDevcie) // hello函数处理"/hello"请求 router.GET("/Gateway4G/ListDevices", ListDevices) // 指定地址和端口号 go router.Run(":10001") } func UpdateDevcie(c *gin.Context) { AppID := c.Query("AppID") AppSecret := c.Query("AppSecret") log.Infof("REST on Update device message :%s, %s/n", AppID, AppSecret) if AppID == AppInfo.AppID && AppSecret == AppInfo.AppSecret { msgInd <- "UpdateDevice" c.JSON(http.StatusOK, gin.H{ "code": 200, "success": true, }) } else { c.JSON(http.StatusUnauthorized, gin.H{ "code": 401, "Erorr": "AppID and AppSecret wrong", }) } } func ListDevices(c *gin.Context) { log.Info("REST on list devcies") devlistchn := make(chan interface{}) msgInd <- "ListDevices" msgInd <- devlistchn devlist := <-devlistchn c.IndentedJSON(http.StatusOK, devlist) }