Skip to content

Commit

Permalink
Merge pull request #5 from slashexx/preview
Browse files Browse the repository at this point in the history
Good lord. Updated rust execution code 🦀
  • Loading branch information
slashexx authored Nov 10, 2024
2 parents 161d117 + f9da78f commit 0208cff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
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

0 comments on commit 0208cff

Please sign in to comment.