index.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package api
  2. import (
  3. requstform "Gwen/http/request/api"
  4. "Gwen/http/response"
  5. "Gwen/model"
  6. "Gwen/service"
  7. "github.com/gin-gonic/gin"
  8. "net/http"
  9. "time"
  10. )
  11. type Index struct {
  12. }
  13. // Index 首页
  14. // @Tags 首页
  15. // @Summary 首页
  16. // @Description 首页
  17. // @Accept json
  18. // @Produce json
  19. // @Success 200 {object} response.Response
  20. // @Failure 500 {object} response.Response
  21. // @Router / [get]
  22. func (i *Index) Index(c *gin.Context) {
  23. response.Success(
  24. c,
  25. "Hello Gwen",
  26. )
  27. }
  28. // Heartbeat 心跳
  29. // @Tags 首页
  30. // @Summary 心跳
  31. // @Description 心跳
  32. // @Accept json
  33. // @Produce json
  34. // @Success 200 {object} nil
  35. // @Failure 500 {object} response.Response
  36. // @Router /heartbeat [post]
  37. func (i *Index) Heartbeat(c *gin.Context) {
  38. info := &requstform.PeerInfoInHeartbeat{}
  39. err := c.ShouldBindJSON(info)
  40. if err != nil {
  41. c.JSON(http.StatusOK, gin.H{})
  42. return
  43. }
  44. if info.Uuid == "" {
  45. c.JSON(http.StatusOK, gin.H{})
  46. return
  47. }
  48. peer := service.AllService.PeerService.FindByUuid(info.Uuid)
  49. if peer == nil || peer.RowId == 0 {
  50. c.JSON(http.StatusOK, gin.H{})
  51. return
  52. }
  53. //如果在40s以内则不更新
  54. if time.Now().Unix()-peer.LastOnlineTime > 40 {
  55. upp := &model.Peer{RowId: peer.RowId, LastOnlineTime: time.Now().Unix(), LastOnlineIp: c.ClientIP()}
  56. service.AllService.PeerService.Update(upp)
  57. }
  58. c.JSON(http.StatusOK, gin.H{})
  59. }