-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from CS3219-AY2425S1/solomon/add-execute-javas…
…cript feat: n4 add javascript execution
- Loading branch information
Showing
7 changed files
with
108 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Use a slim Node.js image | ||
FROM node:18-slim | ||
|
||
# Set the working directory | ||
WORKDIR /app | ||
|
||
# Install any dependencies if necessary (you can skip if no dependencies) | ||
# COPY package*.json ./ | ||
# RUN npm install | ||
|
||
# No entry point or CMD needed as you'll provide the command at runtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package node | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func RunJavaScriptCode(code string, input string) (string, string, error) { | ||
cmd := exec.Command( | ||
"docker", "run", "--rm", | ||
"-i", // allows standard input to be passed in | ||
"apps-node-sandbox", // Docker image with Node.js environment | ||
"node", "-e", code, // Runs JavaScript code with Node.js | ||
) | ||
|
||
// Pass input to the JavaScript script | ||
cmd.Stdin = bytes.NewBufferString(input) | ||
|
||
// Capture standard output and error output | ||
var output bytes.Buffer | ||
var errorOutput bytes.Buffer | ||
cmd.Stdout = &output | ||
cmd.Stderr = &errorOutput | ||
|
||
// Run the command | ||
if err := cmd.Run(); err != nil { | ||
return "", fmt.Sprintf("Command execution failed: %s: %v", errorOutput.String(), err), nil | ||
} | ||
|
||
return strings.TrimSuffix(output.String(), "\n"), strings.TrimSuffix(errorOutput.String(), "\n"), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters