Skip to content

Commit 939410f

Browse files
committed
added java execution
1 parent 78b313c commit 939410f

File tree

5 files changed

+88
-60
lines changed

5 files changed

+88
-60
lines changed

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
version: "3.9"
1+
version: '3'
22
services:
33
server:
44
image: server:1.0
55
build: .
66
ports:
7-
- "3000:3000"
7+
- '3000:3000'
88
volumes:
99
- /var/run/docker.sock:/var/run/docker.sock
1010
- my_vol:/executor

executor/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ RUN apt-get update -y
88
RUN apt-get install gcc -y
99
RUN apt-get update -y
1010
RUN apt-get install g++ -y
11+
RUN apt-get update -y
12+
RUN apt install default-jdk -y
13+
RUN apt install openjdk-11-jdk-headless -y
14+
RUN apt install ecj -y
15+
RUN apt install openjdk-8-jdk-headless -y
1116
RUN mkdir app

server/api/controllers/code/controller.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import CodeService from "../../services/code.service";
2-
import { v4 as uuidv4 } from "uuid";
3-
import path from "path";
1+
import CodeService from '../../services/code.service';
2+
import { v4 as uuidv4 } from 'uuid';
3+
import path from 'path';
44

55
export class Controller {
66
async execute(req, res) {
@@ -11,33 +11,33 @@ export class Controller {
1111
const output = await CodeService.execute(code, input, lang, uuidv4());
1212
if (output) {
1313
res.send({
14-
status: "200",
15-
message: "Code Successfully Executed",
16-
output,
14+
status: '200',
15+
message: 'Code Successfully Executed',
16+
output
1717
});
1818
}
1919
} else {
20-
throw { message: "Invalid Input" };
20+
throw { message: 'Invalid Input' };
2121
}
2222
} catch (error) {
2323
res.send({
24-
status: error.status || "500",
25-
message: error.message || "Something Went Wrong",
24+
status: error.status || '500',
25+
message: error.message || 'Something Went Wrong'
2626
});
2727
}
2828
}
2929
async getcwd(req, res) {
3030
try {
3131
res.send({
32-
status: "200",
32+
status: '200',
3333
message:
34-
"Current working directory is : " +
35-
path.join(process.cwd(), "executor"),
34+
'Current working directory is : ' +
35+
path.join(process.cwd(), 'executor')
3636
});
3737
} catch (error) {
3838
res.send({
39-
status: error.status || "500",
40-
message: error.message || "Something Went Wrong",
39+
status: error.status || '500',
40+
message: error.message || 'Something Went Wrong'
4141
});
4242
}
4343
}

server/api/services/code.service.js

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import fs from "fs";
2-
import path from "path";
3-
import util from "util";
4-
import { execFile, spawn, exec } from "child_process";
5-
import ValidationService from "./validation.service";
1+
import fs from 'fs';
2+
import path from 'path';
3+
import util from 'util';
4+
import { execFile, spawn, exec } from 'child_process';
5+
import ValidationService from './validation.service';
66
const ROOT_DIR = `${process.cwd()}`;
7-
const SOURCE_DIR = path.join(ROOT_DIR, "executor");
7+
const SOURCE_DIR = path.join(ROOT_DIR, 'executor');
88
const TARGET_DIR = `/app/codes`;
9-
const IMAGE_NAME = "executor:1.0";
10-
const VOL_NAME = `my_vol`;
11-
// const VOL_NAME = SOURCE_DIR;
9+
const IMAGE_NAME = 'executor:1.0';
10+
//const VOL_NAME = `my_vol`;
11+
const VOL_NAME = SOURCE_DIR;
1212

1313
class CodeService {
1414
async execute(code, input, lang, id) {
1515
try {
16-
!input ? (input = "") : null;
16+
!input ? (input = '') : null;
1717

18-
//validating code
18+
// validating code
1919
// await this.validateCode(code, input, lang, id);
2020
const { isValid, message } = await ValidationService.execute(
2121
code,
@@ -25,7 +25,7 @@ class CodeService {
2525
);
2626
if (!isValid) {
2727
throw {
28-
message,
28+
message
2929
};
3030
}
3131

@@ -61,20 +61,24 @@ class CodeService {
6161
async writeFile(code, lang, input, id) {
6262
let fileName = `${id}code`;
6363
switch (lang) {
64-
case "javascript": {
65-
fileName += ".js";
64+
case 'javascript': {
65+
fileName += '.js';
6666
break;
6767
}
68-
case "c++": {
69-
fileName += ".cpp";
68+
case 'c++': {
69+
fileName += '.cpp';
7070
break;
7171
}
72-
case "python": {
73-
fileName += ".py";
72+
case 'python': {
73+
fileName += '.py';
74+
break;
75+
}
76+
case 'java': {
77+
fileName += '.java';
7478
break;
7579
}
7680
default: {
77-
throw { message: "Invalid language" };
81+
throw { message: 'Invalid language' };
7882
}
7983
}
8084
const write = util.promisify(fs.writeFile);
@@ -84,30 +88,34 @@ class CodeService {
8488
await write(path.join(SOURCE_DIR, `${id}input.txt`), input);
8589
return {
8690
file: fileName,
87-
inputFile: `${id}input.txt`,
91+
inputFile: `${id}input.txt`
8892
};
8993
} catch (error) {
9094
throw { message: error };
9195
}
9296
}
9397

9498
async writeCommand(lang, file, input, id) {
95-
let command = "";
99+
let command = '';
96100
switch (lang) {
97-
case "javascript": {
101+
case 'javascript': {
98102
command = `cd ${TARGET_DIR} && node ${file} < ${input}`;
99103
break;
100104
}
101-
case "c++": {
105+
case 'c++': {
102106
command = `cd ${TARGET_DIR} && g++ -o ${id} ${file} && ./${id} < ${input}`;
103107
break;
104108
}
105-
case "python": {
109+
case 'python': {
106110
command = `cd ${TARGET_DIR} && python ${file} < ${input}`;
107111
break;
108112
}
113+
case 'java': {
114+
command = `cd ${TARGET_DIR} && javac ${file} && java Input < ${input}`;
115+
break;
116+
}
109117
default: {
110-
throw { message: "Invalid language" };
118+
throw { message: 'Invalid language' };
111119
}
112120
}
113121

@@ -123,10 +131,10 @@ class CodeService {
123131
async execChild(runCode, runContainer, id, file, inputFile, lang) {
124132
return new Promise((resolve, reject) => {
125133
const execCont = exec(`${runContainer}`);
126-
execCont.on("error", (err) => {
127-
throw { status: "404", message: err };
134+
execCont.on('error', err => {
135+
throw { status: '404', message: err };
128136
});
129-
execCont.stdout.on("data", () => {
137+
execCont.stdout.on('data', () => {
130138
exec(`${runCode}`, async (error, stdout, stderr) => {
131139
await this.endContainer(id);
132140
await this.deleteFiles(file, inputFile, lang, id);
@@ -141,19 +149,24 @@ class CodeService {
141149
}
142150

143151
async deleteFiles(fileName, inputName, lang, id) {
144-
fs.unlinkSync(path.join(SOURCE_DIR, fileName), (err) => {
152+
fs.unlinkSync(path.join(SOURCE_DIR, fileName), err => {
145153
if (err) throw { message: err };
146154
});
147155
if (inputName) {
148-
fs.unlinkSync(path.join(SOURCE_DIR, inputName), (err) => {
156+
fs.unlinkSync(path.join(SOURCE_DIR, inputName), err => {
149157
if (err) throw { message: err };
150158
});
151159
}
152-
if (lang == "c++") {
153-
fs.unlinkSync(path.join(SOURCE_DIR, id), (err) => {
160+
if (lang == 'c++') {
161+
fs.unlinkSync(path.join(SOURCE_DIR, id), err => {
154162
if (err) throw { message: err };
155163
});
156164
}
165+
if (lang == 'java') {
166+
fs.unlinkSync(path.join(SOURCE_DIR, 'Input.class'), err => {
167+
if (err) throw err;
168+
});
169+
}
157170
}
158171

159172
async endContainer(id) {
@@ -162,7 +175,7 @@ class CodeService {
162175
exec(`${exit}`, (error, stdout, stderr) => {
163176
if (error) {
164177
console.log(error);
165-
} else console.log("Container stoped and deleted");
178+
} else console.log('Container stoped and deleted');
166179
});
167180
}
168181
}

server/api/services/validation.service.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
11
class ValidationService {
22
async execute(code, input, lang, id) {
33
switch (lang) {
4-
case "javascript": {
5-
let words = ["require(", "exports.", "module.exports"];
4+
case 'javascript': {
5+
let words = ['require(', 'exports.', 'module.exports'];
66
// prevent imports
7-
var valid = !words.some((el) => {
7+
var valid = !words.some(el => {
88
return code.includes(el);
99
});
1010
return {
1111
isValid: valid,
12-
message: "You have unacceptable libs imported",
12+
message: 'You have unacceptable libs imported'
1313
};
1414
}
15-
case "python": {
15+
case 'python': {
1616
let reg1 = RegExp(
1717
/\bimport\W+(?:\w+\W+){0,}(?:os|subprocess|importlib)\b/g
1818
);
19-
words = ["open("];
19+
words = ['open('];
2020

2121
if (code.match(reg1)) {
2222
return {
2323
isValid: false,
24-
message: "You have unacceptable libs imported",
24+
message: 'You have unacceptable libs imported'
2525
};
2626
} else if (
27-
words.every((el) => code.toLowerCase().includes(el.toLowerCase()))
27+
words.every(el => code.toLowerCase().includes(el.toLowerCase()))
2828
) {
2929
return {
3030
isValid: false,
31-
message: "You have unacceptable libs imported",
31+
message: 'You have unacceptable libs imported'
3232
};
3333
}
3434
return {
35-
isValid: true,
35+
isValid: true
36+
};
37+
}
38+
case 'java': {
39+
return {
40+
isValid: true
41+
};
42+
}
43+
case 'c++': {
44+
return {
45+
isValid: true
3646
};
3747
}
3848
default: {
3949
return {
4050
isValid: false,
41-
message: "Please select a valid language",
51+
message: 'Please select a valid language'
4252
};
4353
}
4454
}

0 commit comments

Comments
 (0)