piston/api/src/executor/routes.js

73 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-04-23 01:52:50 +02:00
// {"language":"python","version":"3.9.1","files":{"code.py":"print('hello world')"},"args":[],"stdin":"","compile_timeout":10, "run_timeout":3}
2021-02-20 15:13:56 +01:00
// {"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-03-13 06:01:04 +01:00
2021-02-27 00:58:30 +01:00
run_job_validators: [
body('language')
2021-03-13 06:01:04 +01:00
.isString(),
2021-02-27 00:58:30 +01:00
body('version')
2021-03-13 06:01:04 +01:00
.isString(),
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-03-13 06:01:04 +01:00
.isArray(),
2021-02-27 00:58:30 +01:00
body('files.*.name')
2021-03-13 06:01:04 +01:00
.isString()
2021-02-27 00:58:30 +01:00
.bail()
.not()
.contains('/'),
body('files.*.content')
2021-03-13 06:01:04 +01:00
.isString(),
2021-02-27 11:10:54 +01:00
body('compile_timeout')
2021-03-13 06:01:04 +01:00
.isNumeric(),
2021-02-27 11:10:54 +01:00
body('run_timeout')
2021-03-13 06:01:04 +01:00
.isNumeric(),
2021-02-27 00:58:30 +01:00
body('stdin')
2021-03-13 06:01:04 +01:00
.isString(),
2021-02-27 00:58:30 +01:00
body('args')
.isArray(),
body('args.*')
2021-03-13 06:01:04 +01:00
.isString()
2021-02-27 00:58:30 +01:00
],
2021-02-20 15:13:56 +01:00
2021-03-13 06:01:04 +01:00
// POST /jobs
async run_job(req, res) {
2021-02-20 15:13:56 +01:00
const runtime = get_latest_runtime_matching_language_version(req.body.language, req.body.version);
2021-03-13 06:01:04 +01:00
if (runtime === undefined) {
return res
.status(400)
.send({
message: `${req.body.language}-${req.body.version} runtime is unknown`
});
}
2021-02-20 15:13:56 +01:00
2021-03-05 07:29:09 +01:00
const job = new Job({
runtime,
alias: req.body.language,
2021-03-05 07:29:09 +01:00
files: req.body.files,
args: req.body.args,
stdin: req.body.stdin,
timeouts: {
run: req.body.run_timeout,
compile: req.body.compile_timeout
2021-04-23 01:52:50 +02:00
}
2021-03-05 07:29:09 +01:00
});
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();
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
await job.cleanup();
2021-03-13 06:01:04 +01:00
return res
.status(200)
.send(result);
2021-02-20 15:13:56 +01:00
}
2021-03-13 06:01:04 +01:00
};