103 lines
2.6 KiB
Go
103 lines
2.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"beacon/services"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type WorkflowHandler struct {
|
|
service *services.WorkflowService
|
|
}
|
|
|
|
func NewWorkflowHandler() *WorkflowHandler {
|
|
return &WorkflowHandler{
|
|
service: &services.WorkflowService{},
|
|
}
|
|
}
|
|
|
|
// StartWorkflow godoc
|
|
// @Summary Start a workflow
|
|
// @Description Start a new workflow
|
|
// @Tags workflow
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /workflows/start [post]
|
|
func (h *WorkflowHandler) StartWorkflow(c *gin.Context) {
|
|
|
|
err := h.service.Start("13")
|
|
if err != nil {
|
|
c.JSON(500, gin.H{
|
|
"code": -1,
|
|
"message": "Failed to start workflow",
|
|
})
|
|
return
|
|
}
|
|
c.JSON(200, gin.H{
|
|
"message": "Workflow started successfully",
|
|
})
|
|
}
|
|
|
|
// StopWorkflow POST /api/workflow/stop
|
|
func (h *WorkflowHandler) StopWorkflow(c *gin.Context) {
|
|
// 停止工作流的逻辑
|
|
// 这里可以调用 Temporal 的 API 来停止指定的工作流
|
|
// 例如,使用 WorkflowID 或 RunID 来停止工作流
|
|
c.JSON(200, gin.H{
|
|
"message": "Workflow stopped successfully",
|
|
})
|
|
}
|
|
|
|
// GetWorkflowStatus godoc
|
|
// @Summary Get workflow status
|
|
// @Description Get the status of a workflow by ID
|
|
// @Tags workflow
|
|
// @Produce json
|
|
// @Param id path string true "Workflow ID"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /workflows/{id} [get]
|
|
func (h *WorkflowHandler) GetWorkflowStatus(c *gin.Context) {
|
|
workflowID := c.Param("workflowID")
|
|
if workflowID == "" {
|
|
c.JSON(400, gin.H{"error": "Workflow ID is required"})
|
|
return
|
|
}
|
|
|
|
status, err := h.service.GetWorkflowStatus(workflowID)
|
|
if err != nil {
|
|
c.JSON(500, gin.H{"error": "Failed to get workflow status", "details": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(200, gin.H{"status": status})
|
|
}
|
|
|
|
// GetWorkflowResults godoc
|
|
// @Summary Get workflow results
|
|
// @Description Get the results of a workflow by ID
|
|
// @Tags workflow
|
|
// @Produce json
|
|
// @Param id path string true "Workflow ID"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /workflows/{id}/results [get]
|
|
func (h *WorkflowHandler) GetWorkflowResults(c *gin.Context) {
|
|
workflowID := c.Param("workflowID")
|
|
if workflowID == "" {
|
|
c.JSON(400, gin.H{"error": "Workflow ID is required"})
|
|
return
|
|
}
|
|
|
|
results, err := h.service.GetWorkflowResults(workflowID)
|
|
if err != nil {
|
|
c.JSON(500, gin.H{"error": "Failed to get workflow results", "details": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(200, gin.H{"results": results})
|
|
}
|