run.go 763 B

12345678910111213141516171819202122232425262728293031
  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 = make(chan bool, static.GetCoshubGlobalConfigurations().MaxWorkers)
  10. )
  11. func RunSandboxController(c *gin.Context) {
  12. BindRequest(c, func(req struct {
  13. Language string `json:"language" form:"language" binding:"required"`
  14. Code string `json:"code" form:"code" binding:"required"`
  15. }) {
  16. queue <- true
  17. defer func() {
  18. <-queue
  19. }()
  20. switch req.Language {
  21. case "python3":
  22. c.JSON(200, service.RunPython3Code(req.Code))
  23. default:
  24. c.JSON(400, types.ErrorResponse(-400, "unsupported language"))
  25. }
  26. })
  27. }