Skip to content

Commit

Permalink
Рефакторинг проекта на тайпскрипт
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1nbow1 authored Jun 20, 2024
2 parents 8220bf9 + 6f4ff04 commit be2eecd
Show file tree
Hide file tree
Showing 15 changed files with 763 additions and 271 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
425 changes: 237 additions & 188 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@codemirror/lang-python": "^6.1.3",
"@types/node": "^20.14.6",
"@uiw/codemirror-theme-darcula": "^4.21.21",
"@uiw/react-codemirror": "^4.21.21",
"axios": "^1.6.5",
Expand All @@ -22,8 +23,8 @@
"sass": "^1.70.0"
},
"devDependencies": {
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.55.0",
"eslint-plugin-react": "^7.33.2",
Expand Down
64 changes: 36 additions & 28 deletions server/api/index.js → server/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const logger = winston.createLogger({
format: 'DD.MM.YYYY HH:mm:ss',
}),
align(),
printf((info) => `[${info.level}] ${info.timestamp}: ${info.message}`)
printf((info): string => `[${info.level}] ${info.timestamp}: ${info.message}`)
),
transports: [new winston.transports.Console()],
});
Expand All @@ -32,7 +32,7 @@ const problems = client
.db(process.env.DB_NAME)
.collection(process.env.COLLECTION_NAME);

client.connect().then((client) => {
client.connect().then((client): void => {
logger.info('Подключение установлено')
logger.info(client.options.dbName)
});
Expand All @@ -42,32 +42,32 @@ const options = {
pythonOptions: ["-u"],
};

function run(solution, pid = 0) {
fs.writeFile(`../run_${pid}.py`, solution, (err) => {
function run(solution: string, pid: number = 0): Promise<unknown> {
fs.writeFile(`../run_${pid}.py`, solution, (err): void => {
if (err) throw err;
});
return new Promise((resolve, reject) => {
return new Promise((resolve, reject): void => {
const python = spawn("python3", [`../run_${pid}.py`]);

let output = ''
python.stdout.on("data", (data) => {
let output: string = ''
python.stdout.on("data", (data): void => {
output += data.toString()
});

setTimeout(() => {
setTimeout((): void => {
python.stdout.off("data", data => resolve(data.toString()));
resolve(["Превышено время ожидания. Возможно, найдены бесконечные процессы."]);
logger.warn(`pid: ${pid} Найден бесконечный цикл или решение не вывело результат`)
remove(`../run_${pid}.py`)
remove(`../run_0.py`)
python.kill()
}, 2000);
let errors = ''
let errors: string = ''
python.stderr.on("data", (data) => {
errors += data.toString()
});

python.on("close", () => {
python.on("close", (): void => {
if (errors.length > 0) {
reject(errors.split('\n'))
remove(`../run_${pid}.py`)
Expand All @@ -86,9 +86,9 @@ function run(solution, pid = 0) {
})
}

function remove(filename) {
setTimeout(() => {
fs.unlink(filename, function(err) {
function remove(filename: string): void {
setTimeout((): void => {
fs.unlink(filename, function(err): void {
if(err && err.code == 'ENOENT') {
logger.info("Файла не существует");
} else if (err) {
Expand All @@ -100,23 +100,32 @@ function remove(filename) {
}, 1000)
}

app.get('/', (req, res) => {
app.get('/', (req, res): void => {
res.send({"status": "OK"});
});

app.get("/problems", async (req, res) => {
app.get("/problems", async (req, res): Promise<void> => {
logger.info('/problems')
let result = await problems.find({}).toArray();
res.send(result);
});

app.get("/problem_info/:pid", async (req, res) => {
app.get("/problem_info/:pid", async (req: IRequest, res): Promise<void> => {
logger.info(`/problem_info/${req.params.pid}`)
let result = await problems.findOne({ pid: req.params.pid });
res.send(result);
});

app.post('/run', async (req, res) => {
interface IRequest {
body: {
code: string
},
params: {
pid: number
}
}

app.post('/run', async (req: IRequest, res): Promise<void> => {
logger.info(`/run`)
await run(req.body.code)
.then(results => {
Expand All @@ -128,44 +137,43 @@ app.post('/run', async (req, res) => {

})

app.post("/test/:pid", (req, res) => {
app.post("/test/:pid", (req: IRequest, res): void => {
logger.info(`/test/${req.params.pid}`)
run(req.body.code, req.params.pid)
.then(async () => {
.then(async (): Promise<void> => {
const problem = await problems.findOne({ pid: req.params.pid });
const test_cases = problem["test_cases"];

let test_file =
`from run_${req.params.pid} import solution\n`
let test_file = `from run_${req.params.pid} import solution\n`
for (const test of test_cases) {
test_file += 'print(' + test + ')\n'
}
fs.writeFile(`../tests_${req.params.pid}.py`, test_file, (err) => {
fs.writeFile(`../tests_${req.params.pid}.py`, test_file, (err): void => {
if (err) throw err;
});
PythonShell.run(`../tests_${req.params.pid}.py`, options).then(function (result) {
PythonShell.run(`../tests_${req.params.pid}.py`, options).then(function (result): void {
let passing = true
result.forEach((element) => {
result.forEach((element): void => {
if (element !== "True") {
passing = false
}
});
res.send(passing)

})
.catch(function (err) {
.catch(function (err): void {
res.send(err)
});
}).then(() => {
}).then((): void => {
remove(`../tests_${req.params.pid}.py`)
})
.catch(function (err) {
.catch(function (err): void {
res.send(err);
});


});

app.listen(process.env.PORT || PORT, () => {
app.listen(process.env.PORT || PORT, (): void => {
logger.info(`Сервер запущен на порту ${PORT}`);
});
Loading

0 comments on commit be2eecd

Please sign in to comment.