index.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. requstform "github.com/lejianwen/rustdesk-api/http/request/api"
  5. "github.com/lejianwen/rustdesk-api/http/response"
  6. "github.com/lejianwen/rustdesk-api/model"
  7. "github.com/lejianwen/rustdesk-api/service"
  8. "net/http"
  9. "os"
  10. "time"
  11. )
  12. type Index struct {
  13. }
  14. // Index 首页
  15. // @Tags 首页
  16. // @Summary 首页
  17. // @Description 首页
  18. // @Accept json
  19. // @Produce json
  20. // @Success 200 {object} response.Response
  21. // @Failure 500 {object} response.Response
  22. // @Router / [get]
  23. func (i *Index) Index(c *gin.Context) {
  24. response.Success(
  25. c,
  26. "Hello Gwen",
  27. )
  28. }
  29. // Heartbeat 心跳
  30. // @Tags 首页
  31. // @Summary 心跳
  32. // @Description 心跳
  33. // @Accept json
  34. // @Produce json
  35. // @Success 200 {object} nil
  36. // @Failure 500 {object} response.Response
  37. // @Router /heartbeat [post]
  38. func (i *Index) Heartbeat(c *gin.Context) {
  39. info := &requstform.PeerInfoInHeartbeat{}
  40. err := c.ShouldBindJSON(info)
  41. if err != nil {
  42. c.JSON(http.StatusOK, gin.H{})
  43. return
  44. }
  45. if info.Uuid == "" {
  46. c.JSON(http.StatusOK, gin.H{})
  47. return
  48. }
  49. peer := service.AllService.PeerService.FindByUuid(info.Uuid)
  50. if peer == nil || peer.RowId == 0 {
  51. c.JSON(http.StatusOK, gin.H{})
  52. return
  53. }
  54. //如果在40s以内则不更新
  55. if time.Now().Unix()-peer.LastOnlineTime > 40 {
  56. upp := &model.Peer{RowId: peer.RowId, LastOnlineTime: time.Now().Unix(), LastOnlineIp: c.ClientIP()}
  57. service.AllService.PeerService.Update(upp)
  58. }
  59. c.JSON(http.StatusOK, gin.H{})
  60. }
  61. // Version 版本
  62. // @Tags 首页
  63. // @Summary 版本
  64. // @Description 版本
  65. // @Accept json
  66. // @Produce json
  67. // @Success 200 {object} response.Response
  68. // @Failure 500 {object} response.Response
  69. // @Router /version [get]
  70. func (i *Index) Version(c *gin.Context) {
  71. //读取resources/version文件
  72. v, err := os.ReadFile("resources/version")
  73. if err != nil {
  74. response.Fail(c, 101, err.Error())
  75. return
  76. }
  77. response.Success(
  78. c,
  79. string(v),
  80. )
  81. }