-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (63 loc) · 2.43 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
const fs = require('fs'),
path = require('path'),
http = require('http');
const app = require('connect')();
const swaggerTools = require('swagger-tools');
const jsyaml = require('js-yaml');
const serverPort = 3006;
const mongoose = require('mongoose');
const config = require('./config');
function initilizeMongoose(callback) {
mongoose.Promise = Promise;
return mongoose.connect(config.mongo.uri).then(() => {
Object.keys(config.mongo.options).forEach((key) => {
mongoose.set(key, config.mongo.options[key]);
});
mongoose.Types.ObjectId.prototype.view = () => ({ id: this.toString() });
mongoose.connection.on('error', (err) => {
process.exit(-1);
});
}).then(() => {
callback();
});
}
// swaggerRouter configuration
const options = {
swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: path.join(__dirname, './src/adapters/api/controllers'),
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
const spec = fs.readFileSync(path.join(__dirname,'./src/adapters/api/swagger.yaml'), 'utf8');
const swaggerDoc = jsyaml.safeLoad(spec);
const server = http.createServer(app);
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
initilizeMongoose(() => {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', config.express.accessControlUrl);
res.setHeader('Access-Control-Allow-Headers', config.express.accessControlHeaders);
next();
});
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
// Start the server
server.listen(serverPort, function () {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});
});
});
//module.exports = server;
//module.exports = app;
module.exports = {
app,
server
}