Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Good lord. Updated rust execution code 🦀 #5

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions backend/code-execution-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func executeCode(language string, code string) (string, error) {

switch language {
case "go":
fmt.Println("Go file name is :"+tmpFile.Name())
fmt.Println("Go file name is :" + tmpFile.Name())
cmd = exec.Command("go", "run", tmpFile.Name())
case "python":
cmd = exec.Command("python3", tmpFile.Name())
Expand All @@ -62,41 +62,47 @@ func executeCode(language string, code string) (string, error) {
case "rs":
execCmd := exec.Command("rustc", tmpFile.Name())
if err := execCmd.Run(); err != nil {
cmdOutput,_ := execCmd.CombinedOutput()

cmdOutput, _ := execCmd.CombinedOutput()
return string(cmdOutput), err
}

binaryName := tmpFile.Name()[:len(tmpFile.Name())-len(".rs")]
cmd = exec.Command(binaryName)
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
return string(cmdOutput), err
}
return string(cmdOutput), nil

case "java":
tmpFileName := filepath.Join(os.TempDir(), "Main.java")
tmpFile, err = os.Create(tmpFileName)
if err != nil {
return "", err
}
defer os.Remove(tmpFileName)


defer os.Remove(tmpFileName)

if _, err := tmpFile.Write([]byte(code)); err != nil {
return "", err
}
tmpFile.Close()


tmpFile.Close()

fmt.Printf("Compiling with command: %s %s\n", "javac", tmpFileName)
execCmd := exec.Command("javac", tmpFileName)
cmdOutput, err := execCmd.CombinedOutput()
if err != nil {
return string(cmdOutput), err
}

className := "Main"
cmd = exec.Command("java", className)
cmd.Dir = filepath.Dir(tmpFileName)
cmd.Dir = filepath.Dir(tmpFileName)
cmdOutput, err = cmd.CombinedOutput()
if err != nil {
return string(cmdOutput), err
}
return string(cmdOutput), nil

default:
return "", fmt.Errorf("unsupported language: %s", language)
}
Expand All @@ -106,12 +112,11 @@ func executeCode(language string, code string) (string, error) {
}

func enableCors(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "https://codebrewery.vercel.app")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Origin", "https://codebrewery.vercel.app")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
}


func executeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Received %s request for %s\n", r.Method, r.URL.Path)
enableCors(w)
Expand Down
3 changes: 3 additions & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
*.log
*.git
Loading