run.go 823 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/langgenius/dify-sandbox/internal/service"
  5. "github.com/langgenius/dify-sandbox/internal/static"
  6. "github.com/langgenius/dify-sandbox/internal/types"
  7. )
  8. var (
  9. queue chan bool
  10. )
  11. func InitSandBoxQueue() {
  12. if queue == nil {
  13. queue = make(chan bool, static.GetCoshubGlobalConfigurations().MaxWorkers)
  14. }
  15. }
  16. func RunSandboxController(c *gin.Context) {
  17. BindRequest(c, func(req struct {
  18. Language string `json:"language" form:"language" binding:"required"`
  19. Code string `json:"code" form:"code" binding:"required"`
  20. }) {
  21. queue <- true
  22. defer func() {
  23. <-queue
  24. }()
  25. switch req.Language {
  26. case "python3":
  27. c.JSON(200, service.RunPython3Code(req.Code))
  28. default:
  29. c.JSON(400, types.ErrorResponse(-400, "unsupported language"))
  30. }
  31. })
  32. }