safeline.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package handler
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/chaitin/SafeLine/internal/service"
  7. )
  8. type SafelineHandler struct {
  9. safelineService *service.SafelineService
  10. }
  11. func NewSafelineHandler(safelineService *service.SafelineService) *SafelineHandler {
  12. return &SafelineHandler{
  13. safelineService: safelineService,
  14. }
  15. }
  16. // GetInstallerCount
  17. // @Summary get installer count
  18. // @Description get installer count
  19. // @Tags Safeline
  20. // @Accept json
  21. // @Produce json
  22. // @Success 200 {object} service.InstallerCount
  23. // @Router /safeline/count [get]
  24. func (h *SafelineHandler) GetInstallerCount(c *gin.Context) {
  25. count, err := h.safelineService.GetInstallerCount(c)
  26. if err != nil {
  27. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  28. return
  29. }
  30. c.JSON(200, count)
  31. }
  32. type ExistReq struct {
  33. Id string `json:"id"`
  34. Token string `json:"token"`
  35. }
  36. // Exist return ip if id exist
  37. // @Summary get ip if id exist
  38. // @Description get ip if id exist
  39. // @Tags Safeline
  40. // @Accept json
  41. // @Produce json
  42. // @Param body body ExistReq true "body"
  43. // @Success 200 {object} string
  44. // @Router /exist [post]
  45. func (h *SafelineHandler) Exist(c *gin.Context) {
  46. req := &ExistReq{}
  47. if err := c.ShouldBindJSON(req); err != nil {
  48. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  49. return
  50. }
  51. ip, err := h.safelineService.GetExist(c, req.Id, req.Token)
  52. if err != nil {
  53. c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
  54. return
  55. }
  56. c.JSON(200, gin.H{"ip": ip})
  57. }
  58. type BehaviorReq struct {
  59. Source string `json:"source"`
  60. Type service.BehaviorType `json:"type"`
  61. }
  62. // Behavior record user behavior
  63. // @Summary record user behavior
  64. // @Description record user behavior
  65. // @Tags Safeline
  66. // @Accept json
  67. // @Produce json
  68. // @Param body body BehaviorReq true "body"
  69. // @Success 200 {object} string
  70. // @Router /behavior [post]
  71. func (h *SafelineHandler) Behavior(c *gin.Context) {
  72. req := &BehaviorReq{}
  73. if err := c.BindJSON(req); err != nil {
  74. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  75. return
  76. }
  77. if req.Type >= service.BehaviorTypeMax || req.Type <= service.BehaviorTypeMin {
  78. c.JSON(http.StatusBadRequest, gin.H{"error": "invalid behavior type"})
  79. return
  80. }
  81. byteReq, err := json.Marshal(req)
  82. if err != nil {
  83. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  84. return
  85. }
  86. err = h.safelineService.PostBehavior(c, byteReq)
  87. if err != nil {
  88. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  89. return
  90. }
  91. c.JSON(200, gin.H{})
  92. }