Clean up and add PORT option for open-api serve

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-11-03 16:53:20 +01:00
parent 44768b9614
commit aebfde46de
3 changed files with 20 additions and 14 deletions

View File

@ -25,6 +25,7 @@
"eslint-plugin-redux-saga": "^1.1.0",
"execa": "^1.0.0",
"fs-extra": "9.0.1",
"get-port": "5.1.1",
"glob": "7.1.6",
"husky": "^3.0.0",
"istanbul": "~0.4.2",

View File

@ -1,12 +1,13 @@
/* eslint-disable */
'use strict';
const getPort = require('get-port');
const http = require('http');
const path = require('path');
const fse = require('fs-extra');
const koa = require('koa');
const koaStatic = require('koa-static');
const getPort = require('get-port');
const config = {
port: parseInt(process.env.PORT, 10) || 1339,
};
const args = process.argv.slice(2);
@ -16,25 +17,24 @@ if (!args[0]) {
}
async function run() {
const app = new koa();
const openAPISpecPath = path.join(__dirname, '../../packages', args[0], 'oas.yml');
const indexPagePath = path.join(__dirname, 'public', 'index.html');
if (!(await fse.pathExists(openAPISpecPath))) {
throw new Error(`No OAS configuration found at ${openAPISpecPath}`);
}
app.use(koaStatic(path.join(__dirname, 'public')));
app.use(ctx => {
if (ctx.path === '/spec.yml') {
ctx.body = fse.createReadStream(openAPISpecPath);
const server = http.createServer((req, res) => {
if (req.url == '/spec.yml') {
return fse.createReadStream(openAPISpecPath).pipe(res);
}
return fse.createReadStream(indexPagePath).pipe(res);
});
const port = await getPort({ port: 1339 });
const port = await getPort({ port: config.port });
app.listen(port, () => {
server.listen(port, () => {
console.log(`Server available at http://localhost:${port}`);
});
}

View File

@ -8539,6 +8539,11 @@ get-pkg-repo@^1.0.0:
parse-github-repo-url "^1.3.0"
through2 "^2.0.0"
get-port@5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/get-port/-/get-port-5.1.1.tgz#0469ed07563479de6efb986baf053dcd7d4e3193"
integrity sha512-g/Q1aTSDOxFpchXC4i8ZWvxA1lnPqx/JHqcpIw0/LX9T8x/GBbi6YnlN5nhaKIFkT8oFsscUKgDJYxfwfS6QsQ==
get-port@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/get-port/-/get-port-4.2.0.tgz#e37368b1e863b7629c43c5a323625f95cf24b119"