Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 27, 2021. It is now read-only.

Commit fb75c48

Browse files
committed
added experimented code for online judge
1 parent 162bf95 commit fb75c48

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.cache
22
.cache/
33
node_modules
4+
package-lock.json

openrank-backend/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express';
22
import cors from 'cors';
33
import routes from './routes'
44

5-
console.log('Starting OpenRank Server');
5+
console.log('Starting OpenRank API Server');
66

77
const app = express();
88
app.use(cors());

openrank-execution/.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env"
4+
]
5+
}

openrank-execution/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
temp.*

openrank-execution/package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "openrank-execution",
3+
"version": "1.0.0",
4+
"description": "server/instance for execution of user submitted code on openrank platform",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"start": "nodemon --exec babel-node src/index.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/coderplex/OpenRank.git"
13+
},
14+
"keywords": [
15+
"openrank",
16+
"execution",
17+
"code",
18+
"submission"
19+
],
20+
"author": "Coderplex",
21+
"license": "ISC",
22+
"dependencies": {
23+
"cors": "^2.8.5",
24+
"express": "^4.17.1",
25+
"node-cmd": "^3.0.0"
26+
},
27+
"devDependencies": {
28+
"@babel/core": "^7.5.5",
29+
"@babel/node": "^7.5.5",
30+
"@babel/preset-env": "^7.5.5",
31+
"nodemon": "^1.19.1"
32+
}
33+
}

openrank-execution/src/index.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import express from "express";
2+
import cors from "cors";
3+
// import routes from './routes'
4+
5+
const fs = require("fs");
6+
const { exec } = require("child_process");
7+
8+
console.log("Starting OpenRank Execution Server");
9+
10+
const app = express();
11+
app.use(cors());
12+
app.use(express.urlencoded({ extended: true }));
13+
app.use(express.json());
14+
15+
//Add handlers for routes here
16+
17+
//Lang specific meta, we have to configure this manually for now
18+
const langMeta = {
19+
python3: {
20+
extension: "py",
21+
command: "py",
22+
interpreted: true
23+
},
24+
python2: {
25+
extension: "py",
26+
command: "python",
27+
interpreted: true
28+
},
29+
c: {
30+
extension: "c",
31+
command: "",
32+
interpreted: false,
33+
compile: "gcc"
34+
},
35+
java: {
36+
extension: "java",
37+
command: "java",
38+
interpreted: false,
39+
compile: "javac"
40+
}
41+
};
42+
43+
app.post("/", (req, res) => {
44+
//NOTE: Experimentation code
45+
/*
46+
This route takes the language and code from request and executes
47+
it by spawning a child process based on the meta-data
48+
available for a given language
49+
*/
50+
const { lang, code } = req.body;
51+
fs.writeFile(`temp.${langMeta[lang].extension}`, code, err => {
52+
if (err) console.log(err);
53+
else console.log("Successfully Written to File.");
54+
});
55+
if (langMeta[lang].interpreted) {
56+
exec(
57+
`${langMeta[lang].command} temp.${langMeta[lang].extension}`,
58+
{ timeout: 1000 },
59+
(err, stdout, stderr) => res.send({ err, stderr, stdout })
60+
);
61+
} else {
62+
exec(
63+
`${langMeta[lang].compile} temp.${langMeta[lang].extension}`,
64+
(err, stdout, stderr) => {
65+
if (stderr == null && err == null) {
66+
exec(
67+
`${langMeta[lang].command} ./temp.${langMeta[lang].extension}`,
68+
{ timeout: 1000 },
69+
(err, stdout, stderr) => res.send({ err, stderr, stdout })
70+
);
71+
} else {
72+
res.send({ err, stderr, stdout });
73+
}
74+
}
75+
);
76+
}
77+
//TODO: Add logic to compare output with testcases saved in db before sending the response.
78+
});
79+
80+
app.listen(8001, () => {
81+
console.log("Example app listening on port 8001!");
82+
});

0 commit comments

Comments
 (0)