Merge master
This commit is contained in:
commit
3d61d10373
|
@ -2,3 +2,4 @@ data/
|
||||||
.piston_env
|
.piston_env
|
||||||
node_modules
|
node_modules
|
||||||
result
|
result
|
||||||
|
.vscode/
|
||||||
|
|
|
@ -2,57 +2,6 @@ const fss = require('fs');
|
||||||
const Logger = require('logplease');
|
const Logger = require('logplease');
|
||||||
const logger = Logger.create('config');
|
const logger = Logger.create('config');
|
||||||
|
|
||||||
function parse_overrides(overrides) {
|
|
||||||
try {
|
|
||||||
return JSON.parse(overrides);
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function validate_overrides(overrides, options) {
|
|
||||||
for (const language in overrides) {
|
|
||||||
for (const key in overrides[language]) {
|
|
||||||
if (
|
|
||||||
![
|
|
||||||
'max_process_count',
|
|
||||||
'max_open_files',
|
|
||||||
'max_file_size',
|
|
||||||
'compile_memory_limit',
|
|
||||||
'run_memory_limit',
|
|
||||||
'compile_timeout',
|
|
||||||
'run_timeout',
|
|
||||||
'output_max_size',
|
|
||||||
].includes(key)
|
|
||||||
) {
|
|
||||||
logger.error(`Invalid overridden option: ${key}`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const option = options.find(o => o.key === key);
|
|
||||||
const parser = option.parser;
|
|
||||||
const raw = overrides[language][key];
|
|
||||||
const value = parser(raw);
|
|
||||||
const validators = option.validators;
|
|
||||||
for (const validator of validators) {
|
|
||||||
const response = validator(value, raw);
|
|
||||||
if (response !== true) {
|
|
||||||
logger.error(
|
|
||||||
`Failed to validate overridden option: ${key}`,
|
|
||||||
response
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
overrides[language][key] = value;
|
|
||||||
}
|
|
||||||
// Modifies the reference
|
|
||||||
options[
|
|
||||||
options.index_of(options.find(o => o.key === 'limit_overrides'))
|
|
||||||
] = overrides;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = [
|
const options = [
|
||||||
{
|
{
|
||||||
key: 'log_level',
|
key: 'log_level',
|
||||||
|
@ -68,7 +17,7 @@ const options = [
|
||||||
{
|
{
|
||||||
key: 'bind_address',
|
key: 'bind_address',
|
||||||
desc: 'Address to bind REST API on',
|
desc: 'Address to bind REST API on',
|
||||||
default: `0.0.0.0:${process.env["PORT"] || 2000}`,
|
default: `0.0.0.0:${process.env['PORT'] || 2000}`,
|
||||||
validators: [],
|
validators: [],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -180,7 +129,7 @@ const options = [
|
||||||
key: 'runtime_set',
|
key: 'runtime_set',
|
||||||
desc: 'Key on the flake specified by flake_path to access runtimes from',
|
desc: 'Key on the flake specified by flake_path to access runtimes from',
|
||||||
default: 'all',
|
default: 'all',
|
||||||
validators: []
|
validators: [],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'max_concurrent_jobs',
|
key: 'max_concurrent_jobs',
|
||||||
|
@ -197,53 +146,110 @@ const options = [
|
||||||
default: {},
|
default: {},
|
||||||
parser: parse_overrides,
|
parser: parse_overrides,
|
||||||
validators: [
|
validators: [
|
||||||
x => !!x || `Invalid JSON format for the overrides\n${x}`,
|
x => !!x || `Failed to parse the overrides\n${x}`,
|
||||||
(overrides, _, options) =>
|
validate_overrides,
|
||||||
validate_overrides(overrides, options) ||
|
|
||||||
`Failed to validate the overrides`,
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
logger.info(`Loading Configuration from environment`);
|
Object.freeze(options);
|
||||||
|
|
||||||
let errored = false;
|
function apply_validators(validators, validator_parameters) {
|
||||||
|
for (const validator of validators) {
|
||||||
|
const validation_response = validator(...validator_parameters);
|
||||||
|
if (validation_response !== true) {
|
||||||
|
return validation_response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse_overrides(overrides_string) {
|
||||||
|
function get_parsed_json_or_null(overrides) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(overrides);
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const overrides = get_parsed_json_or_null(overrides_string);
|
||||||
|
if (typeof overrides === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const parsed_overrides = {};
|
||||||
|
for (const language in overrides) {
|
||||||
|
parsed_overrides[language] = {};
|
||||||
|
for (const key in overrides[language]) {
|
||||||
|
if (
|
||||||
|
![
|
||||||
|
'max_process_count',
|
||||||
|
'max_open_files',
|
||||||
|
'max_file_size',
|
||||||
|
'compile_memory_limit',
|
||||||
|
'run_memory_limit',
|
||||||
|
'compile_timeout',
|
||||||
|
'run_timeout',
|
||||||
|
'output_max_size',
|
||||||
|
].includes(key)
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Find the option for the override
|
||||||
|
const option = options.find(o => o.key === key);
|
||||||
|
const parser = option.parser;
|
||||||
|
const raw = overrides[language][key];
|
||||||
|
const value = parser(raw);
|
||||||
|
parsed_overrides[language][key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parsed_overrides;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_overrides(overrides) {
|
||||||
|
for (const language in overrides) {
|
||||||
|
for (const key in overrides[language]) {
|
||||||
|
const value = overrides[language][key];
|
||||||
|
const option = options.find(o => o.key === key);
|
||||||
|
const validators = option.validators;
|
||||||
|
const validation_response = apply_validators(validators, [
|
||||||
|
value,
|
||||||
|
value,
|
||||||
|
]);
|
||||||
|
if (validation_response !== true) {
|
||||||
|
return `In overridden option ${key} for ${language}, ${validation_response}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`Loading Configuration from environment`);
|
||||||
|
|
||||||
let config = {};
|
let config = {};
|
||||||
|
|
||||||
options.forEach(option => {
|
options.forEach(option => {
|
||||||
const env_key = 'PISTON_' + option.key.to_upper_case();
|
const env_key = 'PISTON_' + option.key.to_upper_case();
|
||||||
|
|
||||||
const parser = option.parser || (x => x);
|
const parser = option.parser || (x => x);
|
||||||
|
|
||||||
const env_val = process.env[env_key];
|
const env_val = process.env[env_key];
|
||||||
|
|
||||||
const parsed_val = parser(env_val);
|
const parsed_val = parser(env_val);
|
||||||
|
|
||||||
const value = env_val === undefined ? option.default : parsed_val;
|
const value = env_val === undefined ? option.default : parsed_val;
|
||||||
|
const validator_parameters =
|
||||||
option.validators.for_each(validator => {
|
env_val === undefined ? [value, value] : [parsed_val, env_val];
|
||||||
let response = null;
|
const validation_response = apply_validators(
|
||||||
if (env_val) response = validator(parsed_val, env_val, options);
|
option.validators,
|
||||||
else response = validator(value, value, options);
|
validator_parameters
|
||||||
|
);
|
||||||
if (response !== true) {
|
if (validation_response !== true) {
|
||||||
errored = true;
|
logger.error(
|
||||||
logger.error(
|
`Config option ${option.key} failed validation:`,
|
||||||
`Config option ${option.key} failed validation:`,
|
validation_response
|
||||||
response
|
);
|
||||||
);
|
process.exit(1);
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
config[option.key] = value;
|
config[option.key] = value;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (errored) {
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info('Configuration successfully loaded');
|
logger.info('Configuration successfully loaded');
|
||||||
|
|
||||||
module.exports = config;
|
module.exports = config;
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# build octave as dependency
|
||||||
|
source ../../octave/6.2.0/build.sh
|
||||||
|
|
||||||
|
# curl MATL 22.5.0
|
||||||
|
curl -L "https://github.com/lmendo/MATL/archive/refs/tags/22.5.0.tar.gz" -o MATL.tar.xz
|
||||||
|
tar xf MATL.tar.xz --strip-components=1
|
||||||
|
rm MATL.tar.xz
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# Path to MATL binary
|
||||||
|
export PATH=$PWD/bin:$PATH
|
||||||
|
export MATL_PATH=$PWD
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"language": "matl",
|
||||||
|
"version": "22.5.0",
|
||||||
|
"aliases": []
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# get file as first argument
|
||||||
|
file="$1"
|
||||||
|
|
||||||
|
# remove the file from $@
|
||||||
|
shift
|
||||||
|
|
||||||
|
# use the rest of the arguments as stdin
|
||||||
|
stdin=`printf "%s\n" "$@"`
|
||||||
|
|
||||||
|
# pass stdin into octave which will run MATL
|
||||||
|
echo "$stdin" | octave -W -p "$MATL_PATH" --eval "matl -of '$file'"
|
|
@ -0,0 +1 @@
|
||||||
|
'OK'
|
47
readme.md
47
readme.md
|
@ -239,32 +239,32 @@ Content-Type: application/json
|
||||||
`POST /api/v2/execute`
|
`POST /api/v2/execute`
|
||||||
This endpoint requests execution of some arbitrary code.
|
This endpoint requests execution of some arbitrary code.
|
||||||
|
|
||||||
- `language` (**required**) The language to use for execution, must be a string and must be installed.
|
- `language` (**required**) The language to use for execution, must be a string and must be installed.
|
||||||
- `files` (**required**) An array of files containing code or other data that should be used for execution. The first file in this array is considered the main file.
|
- `files` (**required**) An array of files containing code or other data that should be used for execution. The first file in this array is considered the main file.
|
||||||
- `files[].name` (_optional_) The name of the file to upload, must be a string containing no path or left out.
|
- `files[].name` (_optional_) The name of the file to upload, must be a string containing no path or left out.
|
||||||
- `files[].content` (**required**) The content of the files to upload, must be a string containing text to write.
|
- `files[].content` (**required**) The content of the files to upload, must be a string containing text to write.
|
||||||
- `stdin` (_optional_) The text to pass as stdin to the program. Must be a string or left out. Defaults to blank string.
|
- `stdin` (_optional_) The text to pass as stdin to the program. Must be a string or left out. Defaults to blank string.
|
||||||
- `args` (_optional_) The arguments to pass to the program. Must be an array or left out. Defaults to `[]`.
|
- `args` (_optional_) The arguments to pass to the program. Must be an array or left out. Defaults to `[]`.
|
||||||
- `compile_timeout` (_optional_) The maximum time allowed for the compile stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `10000` (10 seconds).
|
- `compile_timeout` (_optional_) The maximum time allowed for the compile stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `10000` (10 seconds).
|
||||||
- `run_timeout` (_optional_) The maximum time allowed for the run stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `3000` (3 seconds).
|
- `run_timeout` (_optional_) The maximum time allowed for the run stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `3000` (3 seconds).
|
||||||
- `compile_memory_limit` (_optional_) The maximum amount of memory the compile stage is allowed to use in bytes. Must be a number or left out. Defaults to `-1` (no limit)
|
- `compile_memory_limit` (_optional_) The maximum amount of memory the compile stage is allowed to use in bytes. Must be a number or left out. Defaults to `-1` (no limit)
|
||||||
- `run_memory_limit` (_optional_) The maximum amount of memory the run stage is allowed to use in bytes. Must be a number or left out. Defaults to `-1` (no limit)
|
- `run_memory_limit` (_optional_) The maximum amount of memory the run stage is allowed to use in bytes. Must be a number or left out. Defaults to `-1` (no limit)
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"language": "js",
|
"language": "js",
|
||||||
"files": [
|
"files": [
|
||||||
{
|
{
|
||||||
"name": "my_cool_code.js",
|
"name": "my_cool_code.js",
|
||||||
"content": "console.log(process.argv)"
|
"content": "console.log(process.argv)"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"stdin": "",
|
"stdin": "",
|
||||||
"args": ["1", "2", "3"],
|
"args": ["1", "2", "3"],
|
||||||
"compile_timeout": 10000,
|
"compile_timeout": 10000,
|
||||||
"run_timeout": 3000,
|
"run_timeout": 3000,
|
||||||
"compile_memory_limit": -1,
|
"compile_memory_limit": -1,
|
||||||
"run_memory_limit": -1
|
"run_memory_limit": -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -350,6 +350,7 @@ Content-Type: application/json
|
||||||
`llvm_ir`,
|
`llvm_ir`,
|
||||||
`lolcode`,
|
`lolcode`,
|
||||||
`lua`,
|
`lua`,
|
||||||
|
`matl`,
|
||||||
`nasm`,
|
`nasm`,
|
||||||
`nasm64`,
|
`nasm64`,
|
||||||
`nim`,
|
`nim`,
|
||||||
|
|
Loading…
Reference in New Issue