safeline.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package handler
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/chaitin/SafeLine/internal/service"
  6. )
  7. type SafelineHandler struct {
  8. safelineService *service.SafelineService
  9. }
  10. func NewSafelineHandler(safelineService *service.SafelineService) *SafelineHandler {
  11. return &SafelineHandler{
  12. safelineService: safelineService,
  13. }
  14. }
  15. // GetInstallerCount
  16. // @Summary get installer count
  17. // @Description get installer count
  18. // @Tags Safeline
  19. // @Accept json
  20. // @Produce json
  21. // @Success 200 {object} service.InstallerCount
  22. // @Router /safeline/count [get]
  23. func (h *SafelineHandler) GetInstallerCount(c *gin.Context) {
  24. count, err := h.safelineService.GetInstallerCount(c)
  25. if err != nil {
  26. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  27. return
  28. }
  29. c.JSON(200, count)
  30. }
  31. type ExistReq struct {
  32. Id string `json:"id"`
  33. Token string `json:"token"`
  34. }
  35. // Exist return ip if id exist
  36. // @Summary get ip if id exist
  37. // @Description get ip if id exist
  38. // @Tags Safeline
  39. // @Accept json
  40. // @Produce json
  41. // @Param body body ExistReq true "body"
  42. // @Success 200 {object} string
  43. // @Router /exist [post]
  44. func (h *SafelineHandler) Exist(c *gin.Context) {
  45. req := &ExistReq{}
  46. if err := c.ShouldBindJSON(req); err != nil {
  47. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  48. return
  49. }
  50. ip, err := h.safelineService.GetExist(c, req.Id, req.Token)
  51. if err != nil {
  52. c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
  53. return
  54. }
  55. c.JSON(200, gin.H{"ip": ip})
  56. }