piston/api/src/index.js

112 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-02-20 15:13:56 +01:00
#!/usr/bin/env node
2021-02-20 23:39:03 +01:00
require('nocamel');
const Logger = require('logplease');
const express = require('express');
const globals = require('./globals');
const config = require('./config');
const path = require('path');
const fs = require('fs/promises');
const fss = require('fs');
const body_parser = require('body-parser');
const runtime = require('./runtime');
2021-03-13 06:01:04 +01:00
const { validationResult } = require('express-validator');
2021-02-20 23:39:03 +01:00
const logger = Logger.create('index');
2021-02-20 15:13:56 +01:00
const app = express();
(async () => {
2021-02-20 23:39:03 +01:00
logger.info('Setting loglevel to',config.log_level);
2021-03-13 06:01:04 +01:00
Logger.setLogLevel(config.log_level);
2021-02-20 23:39:03 +01:00
logger.debug('Ensuring data directories exist');
2021-03-13 06:01:04 +01:00
Object.values(globals.data_directories).for_each(dir => {
let data_path = path.join(config.data_directory, dir);
2021-02-20 23:39:03 +01:00
logger.debug(`Ensuring ${data_path} exists`);
2021-03-13 06:01:04 +01:00
if (!fss.exists_sync(data_path)) {
2021-02-20 23:39:03 +01:00
logger.info(`${data_path} does not exist.. Creating..`);
2021-03-13 06:01:04 +01:00
try {
2021-02-20 23:39:03 +01:00
fss.mkdir_sync(data_path);
2021-03-13 06:01:04 +01:00
} catch(e) {
logger.error(`Failed to create ${data_path}: `, e.message);
2021-02-20 15:13:56 +01:00
}
}
2021-02-20 23:39:03 +01:00
});
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
logger.info('Loading packages');
const pkgdir = path.join(config.data_directory,globals.data_directories.packages);
2021-02-20 15:13:56 +01:00
2021-03-05 07:29:09 +01:00
const pkglist = await fs.readdir(pkgdir);
2021-03-13 06:01:04 +01:00
2021-03-05 07:29:09 +01:00
const languages = await Promise.all(
pkglist.map(lang=>
fs.readdir(path.join(pkgdir,lang))
.then(x=>x.map(y=>path.join(pkgdir, lang, y)))
));
2021-03-13 06:01:04 +01:00
const installed_languages = languages
.flat()
.filter(pkg => fss.exists_sync(path.join(pkg, globals.pkg_installed_file)));
2021-02-20 15:13:56 +01:00
2021-03-05 07:29:09 +01:00
installed_languages.forEach(pkg => new runtime.Runtime(pkg));
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
logger.info('Starting API Server');
logger.debug('Constructing Express App');
2021-02-27 00:58:30 +01:00
logger.debug('Registering middleware');
2021-02-20 15:13:56 +01:00
2021-03-13 06:01:04 +01:00
app.use(body_parser.urlencoded({ extended: true }));
2021-02-27 00:58:30 +01:00
app.use(body_parser.json());
2021-02-20 15:13:56 +01:00
2021-03-13 06:01:04 +01:00
const validate = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res
.status(422)
.send({
message: errors.array()
});
}
2021-02-20 15:13:56 +01:00
2021-02-27 00:58:30 +01:00
next();
2021-03-13 06:01:04 +01:00
};
2021-02-20 15:13:56 +01:00
2021-02-27 00:58:30 +01:00
logger.debug('Registering Routes');
2021-02-20 15:13:56 +01:00
2021-02-27 00:58:30 +01:00
const ppman_routes = require('./ppman/routes');
2021-03-05 07:29:09 +01:00
const executor_routes = require('./executor/routes');
2021-02-27 00:58:30 +01:00
2021-03-13 06:01:04 +01:00
app.get('/packages', ppman_routes.package_list);
app.post('/packages/:language/:version', ppman_routes.package_install);
app.delete('/packages/:language/:version', ppman_routes.package_uninstall);
2021-03-05 07:29:09 +01:00
app.post('/jobs',
executor_routes.run_job_validators,
validate,
2021-03-13 06:01:04 +01:00
executor_routes.run_job
);
app.get('/runtimes', (req, res) => {
const runtimes = runtime
.map(rt => {
return {
language: rt.language,
version: rt.version.raw,
author: rt.author,
aliases: rt.aliases
};
});
return res
.status(200)
.send(runtimes);
});
2021-02-27 13:25:10 +01:00
2021-02-20 23:39:03 +01:00
logger.debug('Calling app.listen');
2021-03-13 06:01:04 +01:00
const [ address, port ] = config.bind_address.split(':');
2021-02-20 15:13:56 +01:00
2021-03-13 06:01:04 +01:00
app.listen(port, address, () => {
2021-02-20 23:39:03 +01:00
logger.info('API server started on', config.bind_address);
});
2021-03-13 06:01:04 +01:00
})();