jmms
Version:
Jmms cli tools, Jmms is a java meta-micro-service framework
66 lines (53 loc) • 2.05 kB
JavaScript
const _ = require('lodash');
const log = require('../../log');
const utils = require('../../utils');
const fs = require('fs');
const path = require('path');
const proc = require('child_process');
const metas = require('../../metas');
var BaseGenerator = require('../../base');
module.exports = class extends BaseGenerator {
constructor(args, opts) {
super(args, opts, false);
}
run() {
var url;
var port;
if(this.app) {
url = this.app.api.baseUrl + "/swagger.json";
port = this.app.server.port + 1;
}else {
url = "http://localhost:8080/swagger.json";
port = 8081;
}
if(this.options.port) {
port = this.options.port;
}
this._hostSwaggerUI(url, port);
}
_hostSwaggerUI(swaggerUrl, port) {
const express = require('express');
const swaggerUIDir = path.dirname(require.resolve('swagger-ui-dist'));
const swaggerHtml = fs.readFileSync(path.join(__dirname,'../swagger_index.html'),{encoding:'utf-8'})
.replace('http://petstore.swagger.io/v2/swagger.json', swaggerUrl)
.replace('http://localhost:3200/oauth2-redirect.html', 'http://localhost:' + port + '/oauth2-redirect.html')
.replace('</title>', '</title><base href="/">');
const app = express();
app.use('/', serveSwaggerUi);
app.use('/', express.static(swaggerUIDir));
app.listen(port);
log.info(log.chalk.green("\nSwagger UI running at http://localhost:" + port + "\n"));
function serveSwaggerUi(req,res,next)
{
return /^\/?$/.test(req.path)
? res.status(200).send(swaggerHtml)
: next();
};
function servePaiiUi(req,res,next)
{
return /^\/?$/.test(req.path)
? res.status(200).send(paiiHtml)
: next();
};
}
};