Skip to content

Commit

Permalink
added java execution
Browse files Browse the repository at this point in the history
  • Loading branch information
tomarviii88 committed Jan 4, 2021
1 parent 78b313c commit 939410f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 60 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
version: "3.9"
version: '3'
services:
server:
image: server:1.0
build: .
ports:
- "3000:3000"
- '3000:3000'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- my_vol:/executor
Expand Down
5 changes: 5 additions & 0 deletions executor/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ RUN apt-get update -y
RUN apt-get install gcc -y
RUN apt-get update -y
RUN apt-get install g++ -y
RUN apt-get update -y
RUN apt install default-jdk -y
RUN apt install openjdk-11-jdk-headless -y
RUN apt install ecj -y
RUN apt install openjdk-8-jdk-headless -y
RUN mkdir app
28 changes: 14 additions & 14 deletions server/api/controllers/code/controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CodeService from "../../services/code.service";
import { v4 as uuidv4 } from "uuid";
import path from "path";
import CodeService from '../../services/code.service';
import { v4 as uuidv4 } from 'uuid';
import path from 'path';

export class Controller {
async execute(req, res) {
Expand All @@ -11,33 +11,33 @@ export class Controller {
const output = await CodeService.execute(code, input, lang, uuidv4());
if (output) {
res.send({
status: "200",
message: "Code Successfully Executed",
output,
status: '200',
message: 'Code Successfully Executed',
output
});
}
} else {
throw { message: "Invalid Input" };
throw { message: 'Invalid Input' };
}
} catch (error) {
res.send({
status: error.status || "500",
message: error.message || "Something Went Wrong",
status: error.status || '500',
message: error.message || 'Something Went Wrong'
});
}
}
async getcwd(req, res) {
try {
res.send({
status: "200",
status: '200',
message:
"Current working directory is : " +
path.join(process.cwd(), "executor"),
'Current working directory is : ' +
path.join(process.cwd(), 'executor')
});
} catch (error) {
res.send({
status: error.status || "500",
message: error.message || "Something Went Wrong",
status: error.status || '500',
message: error.message || 'Something Went Wrong'
});
}
}
Expand Down
79 changes: 46 additions & 33 deletions server/api/services/code.service.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import fs from "fs";
import path from "path";
import util from "util";
import { execFile, spawn, exec } from "child_process";
import ValidationService from "./validation.service";
import fs from 'fs';
import path from 'path';
import util from 'util';
import { execFile, spawn, exec } from 'child_process';
import ValidationService from './validation.service';
const ROOT_DIR = `${process.cwd()}`;
const SOURCE_DIR = path.join(ROOT_DIR, "executor");
const SOURCE_DIR = path.join(ROOT_DIR, 'executor');
const TARGET_DIR = `/app/codes`;
const IMAGE_NAME = "executor:1.0";
const VOL_NAME = `my_vol`;
// const VOL_NAME = SOURCE_DIR;
const IMAGE_NAME = 'executor:1.0';
//const VOL_NAME = `my_vol`;
const VOL_NAME = SOURCE_DIR;

class CodeService {
async execute(code, input, lang, id) {
try {
!input ? (input = "") : null;
!input ? (input = '') : null;

//validating code
// validating code
// await this.validateCode(code, input, lang, id);
const { isValid, message } = await ValidationService.execute(
code,
Expand All @@ -25,7 +25,7 @@ class CodeService {
);
if (!isValid) {
throw {
message,
message
};
}

Expand Down Expand Up @@ -61,20 +61,24 @@ class CodeService {
async writeFile(code, lang, input, id) {
let fileName = `${id}code`;
switch (lang) {
case "javascript": {
fileName += ".js";
case 'javascript': {
fileName += '.js';
break;
}
case "c++": {
fileName += ".cpp";
case 'c++': {
fileName += '.cpp';
break;
}
case "python": {
fileName += ".py";
case 'python': {
fileName += '.py';
break;
}
case 'java': {
fileName += '.java';
break;
}
default: {
throw { message: "Invalid language" };
throw { message: 'Invalid language' };
}
}
const write = util.promisify(fs.writeFile);
Expand All @@ -84,30 +88,34 @@ class CodeService {
await write(path.join(SOURCE_DIR, `${id}input.txt`), input);
return {
file: fileName,
inputFile: `${id}input.txt`,
inputFile: `${id}input.txt`
};
} catch (error) {
throw { message: error };
}
}

async writeCommand(lang, file, input, id) {
let command = "";
let command = '';
switch (lang) {
case "javascript": {
case 'javascript': {
command = `cd ${TARGET_DIR} && node ${file} < ${input}`;
break;
}
case "c++": {
case 'c++': {
command = `cd ${TARGET_DIR} && g++ -o ${id} ${file} && ./${id} < ${input}`;
break;
}
case "python": {
case 'python': {
command = `cd ${TARGET_DIR} && python ${file} < ${input}`;
break;
}
case 'java': {
command = `cd ${TARGET_DIR} && javac ${file} && java Input < ${input}`;
break;
}
default: {
throw { message: "Invalid language" };
throw { message: 'Invalid language' };
}
}

Expand All @@ -123,10 +131,10 @@ class CodeService {
async execChild(runCode, runContainer, id, file, inputFile, lang) {
return new Promise((resolve, reject) => {
const execCont = exec(`${runContainer}`);
execCont.on("error", (err) => {
throw { status: "404", message: err };
execCont.on('error', err => {
throw { status: '404', message: err };
});
execCont.stdout.on("data", () => {
execCont.stdout.on('data', () => {
exec(`${runCode}`, async (error, stdout, stderr) => {
await this.endContainer(id);
await this.deleteFiles(file, inputFile, lang, id);
Expand All @@ -141,19 +149,24 @@ class CodeService {
}

async deleteFiles(fileName, inputName, lang, id) {
fs.unlinkSync(path.join(SOURCE_DIR, fileName), (err) => {
fs.unlinkSync(path.join(SOURCE_DIR, fileName), err => {
if (err) throw { message: err };
});
if (inputName) {
fs.unlinkSync(path.join(SOURCE_DIR, inputName), (err) => {
fs.unlinkSync(path.join(SOURCE_DIR, inputName), err => {
if (err) throw { message: err };
});
}
if (lang == "c++") {
fs.unlinkSync(path.join(SOURCE_DIR, id), (err) => {
if (lang == 'c++') {
fs.unlinkSync(path.join(SOURCE_DIR, id), err => {
if (err) throw { message: err };
});
}
if (lang == 'java') {
fs.unlinkSync(path.join(SOURCE_DIR, 'Input.class'), err => {
if (err) throw err;
});
}
}

async endContainer(id) {
Expand All @@ -162,7 +175,7 @@ class CodeService {
exec(`${exit}`, (error, stdout, stderr) => {
if (error) {
console.log(error);
} else console.log("Container stoped and deleted");
} else console.log('Container stoped and deleted');
});
}
}
Expand Down
32 changes: 21 additions & 11 deletions server/api/services/validation.service.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
class ValidationService {
async execute(code, input, lang, id) {
switch (lang) {
case "javascript": {
let words = ["require(", "exports.", "module.exports"];
case 'javascript': {
let words = ['require(', 'exports.', 'module.exports'];
// prevent imports
var valid = !words.some((el) => {
var valid = !words.some(el => {
return code.includes(el);
});
return {
isValid: valid,
message: "You have unacceptable libs imported",
message: 'You have unacceptable libs imported'
};
}
case "python": {
case 'python': {
let reg1 = RegExp(
/\bimport\W+(?:\w+\W+){0,}(?:os|subprocess|importlib)\b/g
);
words = ["open("];
words = ['open('];

if (code.match(reg1)) {
return {
isValid: false,
message: "You have unacceptable libs imported",
message: 'You have unacceptable libs imported'
};
} else if (
words.every((el) => code.toLowerCase().includes(el.toLowerCase()))
words.every(el => code.toLowerCase().includes(el.toLowerCase()))
) {
return {
isValid: false,
message: "You have unacceptable libs imported",
message: 'You have unacceptable libs imported'
};
}
return {
isValid: true,
isValid: true
};
}
case 'java': {
return {
isValid: true
};
}
case 'c++': {
return {
isValid: true
};
}
default: {
return {
isValid: false,
message: "Please select a valid language",
message: 'Please select a valid language'
};
}
}
Expand Down

0 comments on commit 939410f

Please sign in to comment.