Execute executes a workflow
(c *gin.Context)
| 344 | |
| 345 | // Execute executes a workflow |
| 346 | func (h *WorkflowHandler) Execute(c *gin.Context) { |
| 347 | ctx := c.Request.Context() |
| 348 | id := c.Param("id") |
| 349 | |
| 350 | var req struct { |
| 351 | Context map[string]any `json:"context"` |
| 352 | Metadata map[string]any `json:"metadata"` |
| 353 | } |
| 354 | |
| 355 | if err := c.ShouldBindJSON(&req); err != nil { |
| 356 | req.Context = make(map[string]any) |
| 357 | } |
| 358 | |
| 359 | // Check if workflow exists |
| 360 | var workflow WorkflowRecord |
| 361 | if err := (*h.store).Get(ctx, "workflows", id, &workflow); err != nil { |
| 362 | if errors.Is(err, store.ErrNotFound) { |
| 363 | c.JSON(http.StatusNotFound, gin.H{ |
| 364 | "success": false, |
| 365 | "error": gin.H{ |
| 366 | "code": "not_found", |
| 367 | "message": "Workflow not found", |
| 368 | }, |
| 369 | }) |
| 370 | return |
| 371 | } |
| 372 | c.JSON(http.StatusInternalServerError, gin.H{ |
| 373 | "success": false, |
| 374 | "error": gin.H{ |
| 375 | "code": "internal_error", |
| 376 | "message": "Failed to get workflow: " + err.Error(), |
| 377 | }, |
| 378 | }) |
| 379 | return |
| 380 | } |
| 381 | |
| 382 | // Create execution record |
| 383 | execution := &WorkflowExecution{ |
| 384 | ID: uuid.New().String(), |
| 385 | WorkflowID: id, |
| 386 | Status: "pending", |
| 387 | StartedAt: time.Now(), |
| 388 | Context: req.Context, |
| 389 | Logs: []ExecutionLog{}, |
| 390 | Metadata: req.Metadata, |
| 391 | } |
| 392 | |
| 393 | if err := (*h.store).Set(ctx, "workflow_executions", execution.ID, execution); err != nil { |
| 394 | c.JSON(http.StatusInternalServerError, gin.H{ |
| 395 | "success": false, |
| 396 | "error": gin.H{ |
| 397 | "code": "internal_error", |
| 398 | "message": "Failed to create execution: " + err.Error(), |
| 399 | }, |
| 400 | }) |
| 401 | return |
| 402 | } |
| 403 |