piston/api/src/executor/routes.js

61 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-02-20 15:13:56 +01:00
// {"language":"python","version":"3.9.1","files":{"code.py":"print('hello world')"},"args":[],"stdin":"","compile_timeout":10, "run_timeout":3, "main": "code.py"}
// {"success":true, "run":{"stdout":"hello world", "stderr":"", "error_code":0},"compile":{"stdout":"","stderr":"","error_code":0}}
2021-02-20 23:39:03 +01:00
const { get_latest_runtime_matching_language_version } = require('../runtime');
const { Job } = require('./job');
2021-02-27 00:58:30 +01:00
const { body } = require('express-validator');
2021-02-20 15:13:56 +01:00
module.exports = {
2021-02-27 00:58:30 +01:00
run_job_validators: [
body('language')
2021-02-27 01:08:51 +01:00
.isString(), // eslint-disable-line snakecasejs/snakecasejs
2021-02-27 00:58:30 +01:00
body('version')
2021-02-27 11:10:54 +01:00
.isString(), // eslint-disable-line snakecasejs/snakecasejs
2021-03-05 07:29:09 +01:00
// isSemVer requires it to be a version, not a selector
2021-02-27 00:58:30 +01:00
body('files')
2021-02-27 01:08:51 +01:00
.isArray(), // eslint-disable-line snakecasejs/snakecasejs
2021-02-27 00:58:30 +01:00
body('files.*.name')
2021-02-27 01:08:51 +01:00
.isString() // eslint-disable-line snakecasejs/snakecasejs
2021-02-27 00:58:30 +01:00
.bail()
.not()
.contains('/'),
body('files.*.content')
2021-02-27 01:08:51 +01:00
.isString(), // eslint-disable-line snakecasejs/snakecasejs
2021-02-27 11:10:54 +01:00
body('compile_timeout')
.isNumeric(), // eslint-disable-line snakecasejs/snakecasejs
body('run_timeout')
2021-02-27 01:08:51 +01:00
.isNumeric(), // eslint-disable-line snakecasejs/snakecasejs
2021-02-27 00:58:30 +01:00
body('stdin')
2021-02-27 01:08:51 +01:00
.isString(), // eslint-disable-line snakecasejs/snakecasejs
2021-02-27 00:58:30 +01:00
body('args')
.isArray(),
body('args.*')
2021-02-27 01:08:51 +01:00
.isString() // eslint-disable-line snakecasejs/snakecasejs
2021-02-27 00:58:30 +01:00
],
2021-02-20 15:13:56 +01:00
async run_job(req, res){
// POST /jobs
2021-02-27 00:58:30 +01:00
2021-02-20 15:13:56 +01:00
const runtime = get_latest_runtime_matching_language_version(req.body.language, req.body.version);
2021-02-20 23:39:03 +01:00
if(runtime == undefined) return res.json_error(`${req.body.language}-${req.body.version} runtime is unknown`, 400);
2021-02-20 15:13:56 +01:00
2021-03-05 07:29:09 +01:00
const job = new Job({
runtime,
files: req.body.files,
args: req.body.args,
stdin: req.body.stdin,
timeouts: {
run: req.body.run_timeout,
compile: req.body.compile_timeout
},
main: req.body.main
});
2021-02-20 23:39:03 +01:00
await job.prime();
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
const result = await job.execute();
res.json_success(result);
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
await job.cleanup();
2021-02-20 15:13:56 +01:00
}
2021-02-20 23:39:03 +01:00
};