初始化
This commit is contained in:
40
server/runner/runner.go
Normal file
40
server/runner/runner.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ExecutionResult 执行结果
|
||||
type ExecutionResult struct {
|
||||
Output string `json:"output"`
|
||||
Error string `json:"error"`
|
||||
Duration int64 `json:"duration"` // 毫秒
|
||||
ExitCode int `json:"exitCode"`
|
||||
}
|
||||
|
||||
// CodeRunner 代码执行接口
|
||||
type CodeRunner interface {
|
||||
Run(ctx context.Context, code string) (*ExecutionResult, error)
|
||||
}
|
||||
|
||||
// GetRunner 获取对应语言的运行器
|
||||
func GetRunner(language string) (CodeRunner, error) {
|
||||
tempDir := filepath.Join(os.TempDir(), "art-code-runner")
|
||||
if err := os.MkdirAll(tempDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create temp dir: %v", err)
|
||||
}
|
||||
|
||||
switch language {
|
||||
case "go", "golang":
|
||||
return &GoRunner{TempDir: tempDir}, nil
|
||||
case "php":
|
||||
return &PHPRunner{TempDir: tempDir}, nil
|
||||
case "javascript", "js", "node":
|
||||
return &NodeRunner{TempDir: tempDir}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported language: %s", language)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user