-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Base Python Code * Python Compiler * Update Dockerfile * Update Dockerfile * Python * Java Added * Java Added
- Loading branch information
1 parent
5e15fb2
commit 9af26de
Showing
6 changed files
with
170 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const express = require('express'); | ||
const { javaCompile} = require('../compiler/java') | ||
const router = express.Router(); | ||
|
||
// | ||
router.post('/', async (req, res) => { | ||
const InputCode = Buffer.from(req.body.code, 'base64').toString('binary') | ||
const DeCode = Buffer.from(req.body.input, 'base64').toString('binary') | ||
const CentralClass = req?.headers?.class || "Main" | ||
let response = await javaCompile(InputCode, DeCode, CentralClass); | ||
if (response.statusMes === "Compiler Error") { | ||
res.status(202).json(response) | ||
} else if (response.statusMes === "Run Time Error") { | ||
res.status(201).json(response) | ||
} else { | ||
res.status(200).json(response) | ||
} | ||
|
||
}); | ||
|
||
|
||
|
||
|
||
module.exports = router; |
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,138 @@ | ||
const { v4: uuid } = require('uuid'); | ||
const { exec } = require('child_process'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const saveFile = (file, data) => { | ||
return new Promise((resolve, reject) => { | ||
fs.writeFile(file, data, function (err) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve([file]); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
async function deleteFiles(javaPath, inputPath, exePath) { | ||
if (fs.existsSync(javaPath)) { | ||
await fs.unlinkSync(javaPath); | ||
} | ||
|
||
if (fs.existsSync(inputPath)) { | ||
await fs.unlinkSync(inputPath); | ||
} | ||
|
||
if (fs.existsSync(exePath)) { | ||
await fs.unlinkSync(exePath); | ||
} | ||
} | ||
|
||
|
||
|
||
function getExecutablePath(fileName) { | ||
// console.log(os.platform()); | ||
if (os.platform() === 'win32') { | ||
return `${path.join(__dirname, '..', 'upload', fileName)}.exe`; | ||
} | ||
if (os.platform() === 'linux') { | ||
return `${path.join(__dirname, '..', 'upload', fileName)}`; | ||
} | ||
} | ||
|
||
function getRunCommand(input, CentralClass) { | ||
|
||
let path = getExecutablePath("") | ||
return `cd ${path} && \ java ${CentralClass} < ${input}`; | ||
|
||
} | ||
|
||
function getjavaPath(fileName) { | ||
return `${path.join(__dirname, '..', 'upload', fileName)}.java`; | ||
} | ||
|
||
function getInputPath(fileName) { | ||
return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`; | ||
} | ||
|
||
function compileProgram(javaPath) { | ||
return new Promise((resolve, reject) => { | ||
exec(`javac ${javaPath}`, (error, stdout, stderr) => { | ||
if (error) { | ||
console.log({ error, stdout, stderr }) | ||
reject({ error, stdout, stderr }); | ||
} else { | ||
console.log({ error, stdout, stderr }) | ||
resolve({ stdout, stderr }); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function runProgram(inputPath, CentralClass) { | ||
return new Promise((resolve, reject) => { | ||
exec(getRunCommand(inputPath, CentralClass), (error, stdout, stderr) => { | ||
if (error) { | ||
console.log({ error, stdout, stderr }) | ||
reject({ error, stdout, stderr }); | ||
} else { | ||
console.log({ error, stdout, stderr }) | ||
resolve({ stdout, stderr }); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|
||
const javaCompile = async (code, input, CentralClass) => { | ||
console.log({ code, input }) | ||
let state = { | ||
stdout: null, | ||
stderr: null, | ||
statusMes: "", | ||
} | ||
let uniqueFileName = uuid(); | ||
// let executePath = getExecutablePath(uniqueFileName) | ||
let javaPath = getjavaPath(uniqueFileName) | ||
let ipPath = getInputPath(uniqueFileName) | ||
|
||
await saveFile(javaPath, code); | ||
await saveFile(ipPath, input); | ||
|
||
try { | ||
let { stdout, stderr } = await compileProgram(javaPath); | ||
console.log({ stdout, stderr }) | ||
} catch (err) { | ||
state.stderr = err.stderr; | ||
state.statusMes = "Compiler Error"; | ||
deleteFiles(javaPath, ipPath); | ||
return state; | ||
} | ||
|
||
try { | ||
let { stdout, stderr } = await runProgram(ipPath, CentralClass); | ||
state.stdout = stdout; | ||
state.stderr = stderr; | ||
console.log({ stdout, stderr }) | ||
|
||
} catch (err) { | ||
state.stderr = err.stderr; | ||
state.statusMes = "Run Time Error"; | ||
console.log({ state }) | ||
deleteFiles(javaPath, ipPath); | ||
} | ||
|
||
if (state.stderr === '') { | ||
state.stderr = null; | ||
} | ||
state.statusMes = "Successfully Compiled"; | ||
await deleteFiles(javaPath, ipPath); | ||
return state; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
module.exports = { javaCompile }; |
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
Binary file not shown.