From a1eb7324066b0c0bae64a3defd59b8e21fd2212b Mon Sep 17 00:00:00 2001 From: Brian Seymour Date: Thu, 22 Apr 2021 22:27:50 -0500 Subject: [PATCH] update readme, refactor some code --- api/src/executor/routes.js | 2 +- cli/commands/execute.js | 75 ++++++++++++++++++++------------------ readme.md | 38 ++++++++----------- 3 files changed, 56 insertions(+), 59 deletions(-) diff --git a/api/src/executor/routes.js b/api/src/executor/routes.js index c634858..d7cabff 100644 --- a/api/src/executor/routes.js +++ b/api/src/executor/routes.js @@ -19,7 +19,7 @@ module.exports = { .isString(), ], - // POST /jobs + // POST /execute async run_job(req, res) { const runtime = get_latest_runtime_matching_language_version(req.body.language, req.body.version); diff --git a/cli/commands/execute.js b/cli/commands/execute.js index 2cd0595..4ddb221 100644 --- a/cli/commands/execute.js +++ b/cli/commands/execute.js @@ -3,9 +3,9 @@ const fs = require('fs'); const path = require('path'); const chalk = require('chalk'); -exports.command = ['execute [args..]'] -exports.aliases = ['run'] -exports.describe = 'Executes file with the specified runner' +exports.command = ['execute [args..]']; +exports.aliases = ['run']; +exports.describe = 'Executes file with the specified runner'; exports.builder = { languageVersion: { @@ -36,23 +36,22 @@ exports.builder = { array: true, desc: 'Additional files to add', } -} +}; -exports.handler = async function(argv){ - +exports.handler = async function(argv) { const files = [...(argv.files || []),argv.file] - .map(file_path => ({ - name: path.basename(file_path), - content: fs.readFileSync(file_path).toString() - })); - - - const stdin = (argv.stdin && await new Promise((resolve, _)=>{ - var data = ""; - process.stdin.on('data', d=> data += d) - process.stdin.on('end', _ => resolve(data)) - })) || ""; + .map(file_path => { + return { + name: path.basename(file_path), + content: fs.readFileSync(file_path).toString() + }; + }); + const stdin = (argv.stdin && await new Promise((resolve, _) => { + let data = ''; + process.stdin.on('data', d => data += d); + process.stdin.on('end', _ => resolve(data)); + })) || ''; const request = { language: argv.language, @@ -64,35 +63,39 @@ exports.handler = async function(argv){ run_timeout: argv.rt }; - let response = await argv.axios.post('/jobs', request); - response = response.data + let { data: response } = await argv.axios.post('/api/v1/execute', request); - function step(name, ctx){ - console.log(chalk.bold(`== ${name} ==`)) - if(ctx.stdout){ - console.log(" ",chalk.bold(`STDOUT`)) - console.log(" ",ctx.stdout.replace(/\n/g,'\n ')) + const step = (name, ctx) => { + console.log(chalk.bold(`== ${name} ==`)); + + if (ctx.stdout) { + console.log(chalk.bold(`STDOUT`)) + console.log(ctx.stdout.replace(/\n/g,'\n ')) } - if(ctx.stderr){ + + if (ctx.stderr) { console.log(chalk.bold(`STDERR`)) - console.log(" ",ctx.stderr.replace(/\n/g,'\n ')) + console.log(ctx.stderr.replace(/\n/g,'\n ')) } - if(ctx.code) + if (ctx.code) { console.log( chalk.bold(`Exit Code:`), chalk.bold[ctx.code > 0 ? 'red' : 'green'](ctx.code) - ) - if(ctx.signal) + ); + } + + if (ctx.signal) { console.log( chalk.bold(`Signal:`), chalk.bold.yellow(ctx.signal) - ) + ); + } } - - - if(response.compile) step('Compile', response.compile) - step('Run', response.run) - -} \ No newline at end of file + if (response.compile) { + step('Compile', response.compile); + } + + step('Run', response.run); +} diff --git a/readme.md b/readme.md index caf2aa3..726af10 100644 --- a/readme.md +++ b/readme.md @@ -73,20 +73,9 @@ The following are approved and endorsed extensions/utilities to the core Piston
When using the public Piston API, use the base URL: - ``` https://emkc.org/api/v1/piston ``` - -#### GET -``` -https://emkc.org/api/v1/piston/versions -``` -#### POST -``` -https://emkc.org/api/v1/piston/execute -``` - > Important Note: The Piston API is rate limited to 5 requests per second. If you have a need for more requests than that and it's for a good cause, please reach out to me (EngineerMan#0001) on [Discord](https://discord.gg/engineerman) so we can discuss potentially getting you an unlimited key. @@ -129,7 +118,13 @@ cd cli && npm i && cd - ### Installation ```sh -docker run -v $PWD:'/piston' --tmpfs /piston/jobs -dit -p 2000:2000 --name piston_api ghcr.io/engineer-man/piston +docker run \ + -v $PWD:'/piston' \ + --tmpfs /piston/jobs \ + -dit \ + -p 2000:2000 \ + --name piston_api \ + ghcr.io/engineer-man/piston ```
@@ -146,22 +141,21 @@ You can execute the cli with `cli/index.js`. # List all available packages cli/index.js ppman list -# Install python 3.9.1 -cli/index.js ppman install python 3.9.1 - # Install latest python cli/index.js ppman install python -# Run a python script -echo 'print("Hello world!")' > test.py -cli/index.js run python test.py -l 3.9.1 +# Install specific version of python +cli/index.js ppman install python 3.9.4 -# Run the script using the latest version +# Run a python script using the latest version +echo 'print("Hello world!")' > test.py cli/index.js run python test.py -# Run using python 3.x +# Run a python script using a specific version +echo 'print("Hello world!")' > test.py +cli/index.js run python test.py -l 3.9.4 cli/index.js run python test.py -l 3.x - +cli/index.js run python test.py -l 3 ``` If you are operating on a remote machine, add the `-u` flag like so: @@ -178,7 +172,7 @@ This is used by the CLI to carry out running jobs and package management. #### Runtimes Endpoint `GET /api/v1/runtimes` This endpoint will return the supported languages along with the current version and aliases. To execute -code for a particular language using the `/jobs` endpoint, either the name or one of the aliases must +code for a particular language using the `/api/v1/execute` endpoint, either the name or one of the aliases must be provided, along with the version. Multiple versions of the same language may be present at the same time, and may be selected when running a job. ```json