func_detail.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package cron
  2. import (
  3. "net/http"
  4. "github.com/xinliangnote/go-gin-api/internal/code"
  5. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  6. "github.com/xinliangnote/go-gin-api/internal/pkg/validation"
  7. "github.com/xinliangnote/go-gin-api/internal/services/cron"
  8. "github.com/spf13/cast"
  9. )
  10. type detailRequest struct {
  11. Id string `uri:"id"` // HashID
  12. }
  13. type detailResponse struct {
  14. Name string `json:"name"` // 任务名称
  15. Spec string `json:"spec"` // crontab 表达式
  16. Command string `json:"command"` // 执行命令
  17. Protocol int32 `json:"protocol"` // 执行方式 1:shell 2:http
  18. HttpMethod int32 `json:"http_method"` // http 请求方式 1:get 2:post
  19. Timeout int32 `json:"timeout"` // 超时时间(单位:秒)
  20. RetryTimes int32 `json:"retry_times"` // 重试次数
  21. RetryInterval int32 `json:"retry_interval"` // 重试间隔(单位:秒)
  22. NotifyStatus int32 `json:"notify_status"` // 执行结束是否通知 1:不通知 2:失败通知 3:结束通知 4:结果关键字匹配通知
  23. NotifyType int32 `json:"notify_type"` // 通知类型 1:邮件 2:webhook
  24. NotifyReceiverEmail string `json:"notify_receiver_email"` // 通知者邮箱地址(多个用,分割)
  25. NotifyKeyword string `json:"notify_keyword"` // 通知匹配关键字(多个用,分割)
  26. Remark string `json:"remark"` // 备注
  27. IsUsed int32 `json:"is_used"` // 是否启用 1:是 -1:否
  28. }
  29. // Detail 获取单条任务详情
  30. // @Summary 获取单条任务详情
  31. // @Description 获取单条任务详情
  32. // @Tags API.cron
  33. // @Accept json
  34. // @Produce json
  35. // @Param id path string true "hashId"
  36. // @Success 200 {object} detailResponse
  37. // @Failure 400 {object} code.Failure
  38. // @Router /api/cron/{id} [get]
  39. // @Security LoginToken
  40. func (h *handler) Detail() core.HandlerFunc {
  41. return func(ctx core.Context) {
  42. req := new(detailRequest)
  43. res := new(detailResponse)
  44. if err := ctx.ShouldBindURI(req); err != nil {
  45. ctx.AbortWithError(core.Error(
  46. http.StatusBadRequest,
  47. code.ParamBindError,
  48. validation.Error(err)).WithError(err),
  49. )
  50. return
  51. }
  52. ids, err := h.hashids.HashidsDecode(req.Id)
  53. if err != nil {
  54. ctx.AbortWithError(core.Error(
  55. http.StatusBadRequest,
  56. code.HashIdsDecodeError,
  57. code.Text(code.HashIdsDecodeError)).WithError(err),
  58. )
  59. return
  60. }
  61. searchOneData := new(cron.SearchOneData)
  62. searchOneData.Id = cast.ToInt32(ids[0])
  63. info, err := h.cronService.Detail(ctx, searchOneData)
  64. if err != nil {
  65. ctx.AbortWithError(core.Error(
  66. http.StatusBadRequest,
  67. code.CronDetailError,
  68. code.Text(code.CronDetailError)).WithError(err),
  69. )
  70. return
  71. }
  72. res.Name = info.Name
  73. res.Spec = info.Spec
  74. res.Command = info.Command
  75. res.Protocol = info.Protocol
  76. res.HttpMethod = info.HttpMethod
  77. res.Timeout = info.Timeout
  78. res.RetryTimes = info.RetryTimes
  79. res.RetryInterval = info.RetryInterval
  80. res.NotifyStatus = info.NotifyStatus
  81. res.NotifyType = info.NotifyType
  82. res.NotifyReceiverEmail = info.NotifyReceiverEmail
  83. res.NotifyKeyword = info.NotifyKeyword
  84. res.Remark = info.Remark
  85. res.IsUsed = info.IsUsed
  86. ctx.Payload(res)
  87. }
  88. }