-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
51 lines (43 loc) · 1.34 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
// Patched version of swagger-node-runner waiting on https://github.com/theganyo/swagger-node-runner/pull/119
const Runner = require('@openapi-mock/swagger-node-runner');
const express = require('express');
const path = require('path');
const logPaths = require('./lib/logPaths');
function createRunner(config) {
return new Promise(resolve => {
Runner.create(config, function(err, runner) {
if (err) { throw err; }
resolve(runner);
});
});
}
function createServer(app, runner, port) {
const swaggerExpress = runner.expressMiddleware();
swaggerExpress.register(app);
const server = app.listen(port);
logPaths(swaggerExpress.runner.swagger.paths);
console.log(`Mock API running at http://127.0.0.1:${port}${swaggerExpress.runner.swagger.basePath || ''}`);
return server;
};
async function createApp({
spec = path.join(__dirname, './example/spec/petstore.no-key.json'),
mock,
config = path.join(__dirname, '.'),
port = process.env.PORT || 10010} = {}) {
const runnerConfig = {
configDir: config,
swaggerFile: spec,
appRoot: __dirname,
bagpipes: {
_router: {
mockControllersDirs: [mock]
}
}
};
const app = express();
const runner = await createRunner(runnerConfig);
const server = createServer(app, runner, port);
return server;
}
module.exports = createApp