ginmain.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package middleware
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/yuguorong/go/log"
  6. )
  7. type APP_INFO struct {
  8. AppName string
  9. AppID string
  10. AppSecret string
  11. }
  12. var AppInfo = &APP_INFO{
  13. AppName: "PowerMeter",
  14. AppID: "6L1MQezb",
  15. AppSecret: "90ebe5ecbe350923cd00a140ab88739ed2858009",
  16. }
  17. var msgInd chan interface{} = nil
  18. var router *gin.Engine
  19. func RestServerStart(mainloop chan interface{}) {
  20. // Engin
  21. msgInd = mainloop
  22. router = gin.Default()
  23. router.GET("/Gateway4G/UpdateDevcie", UpdateDevcie) // hello函数处理"/hello"请求
  24. router.GET("/Gateway4G/ListDevices", ListDevices)
  25. // 指定地址和端口号
  26. go router.Run(":10001")
  27. }
  28. func UpdateDevcie(c *gin.Context) {
  29. AppID := c.Query("AppID")
  30. AppSecret := c.Query("AppSecret")
  31. log.Infof("REST on Update device message :%s, %s/n", AppID, AppSecret)
  32. if AppID == AppInfo.AppID && AppSecret == AppInfo.AppSecret {
  33. msgInd <- "UpdateDevice"
  34. c.JSON(http.StatusOK, gin.H{
  35. "code": 200,
  36. "success": true,
  37. })
  38. } else {
  39. c.JSON(http.StatusUnauthorized, gin.H{
  40. "code": 401,
  41. "Erorr": "AppID and AppSecret wrong",
  42. })
  43. }
  44. }
  45. func ListDevices(c *gin.Context) {
  46. log.Info("REST on list devcies")
  47. devlistchn := make(chan interface{})
  48. msgInd <- "ListDevices"
  49. msgInd <- devlistchn
  50. devlist := <-devlistchn
  51. c.IndentedJSON(http.StatusOK, devlist)
  52. }