piston/api/src/index.js

107 lines
2.9 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 expressWs = require('express-ws');
2021-02-20 23:39:03 +01:00
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');
const logger = Logger.create('index');
2021-02-20 15:13:56 +01:00
const app = express();
expressWs(app);
2021-02-20 15:13:56 +01:00
(async () => {
2021-05-08 02:30:40 +02:00
logger.info('Setting loglevel to', config.log_level);
Logger.setLogLevel(config.log_level);
logger.debug('Ensuring data directories exist');
Object.values(globals.data_directories).for_each(dir => {
let data_path = path.join(config.data_directory, dir);
logger.debug(`Ensuring ${data_path} exists`);
if (!fss.exists_sync(data_path)) {
logger.info(`${data_path} does not exist.. Creating..`);
try {
fss.mkdir_sync(data_path);
} catch (e) {
logger.error(`Failed to create ${data_path}: `, e.message);
}
}
});
fss.chmodSync(
path.join(config.data_directory, globals.data_directories.jobs),
0o711
);
2021-05-08 02:30:40 +02:00
logger.info('Loading packages');
const pkgdir = path.join(
config.data_directory,
globals.data_directories.packages
);
2021-03-13 06:01:04 +01:00
2021-05-08 02:30:40 +02:00
const pkglist = await fs.readdir(pkgdir);
2021-02-20 15:13:56 +01:00
2021-05-08 02:30:40 +02:00
const languages = await Promise.all(
pkglist.map(lang => {
return fs.readdir(path.join(pkgdir, lang)).then(x => {
return x.map(y => path.join(pkgdir, lang, y));
});
})
);
const installed_languages = languages
.flat()
.filter(pkg =>
fss.exists_sync(path.join(pkg, globals.pkg_installed_file))
);
installed_languages.for_each(pkg => runtime.load_package(pkg));
2021-02-20 15:13:56 +01:00
2021-05-08 02:30:40 +02:00
logger.info('Starting API Server');
logger.debug('Constructing Express App');
logger.debug('Registering middleware');
2021-02-20 15:13:56 +01:00
2021-05-08 02:30:40 +02:00
app.use(body_parser.urlencoded({ extended: true }));
app.use(body_parser.json());
app.use((err, req, res, next) => {
return res.status(400).send({
stack: err.stack,
});
});
2021-04-23 04:47:08 +02:00
2021-05-08 02:30:40 +02:00
logger.debug('Registering Routes');
2021-02-20 15:13:56 +01:00
2021-05-08 02:30:40 +02:00
const api_v2 = require('./api/v2');
app.use('/api/v2', api_v2);
2021-02-27 13:25:10 +01:00
const { version } = require('../package.json');
app.get('/', (req, res, next) => {
return res.status(200).send({ message: `Piston v${version}` });
});
2021-05-08 02:30:40 +02:00
app.use((req, res, next) => {
return res.status(404).send({ message: 'Not Found' });
});
2021-04-23 04:49:12 +02:00
2021-05-08 02:30:40 +02:00
logger.debug('Calling app.listen');
const [address, port] = config.bind_address.split(':');
2021-02-20 15:13:56 +01:00
const server = app.listen(port, address, () => {
2021-05-08 02:30:40 +02:00
logger.info('API server started on', config.bind_address);
});
process.on('SIGTERM', () => {
server.close();
process.exit(0)
});
2021-03-13 06:01:04 +01:00
})();