-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.js
80 lines (60 loc) · 2.23 KB
/
rest.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
80
var http = require('http');
var path = require('path');
var debug = require('debug')('sws:testapp');
// Server
var server = null;
// Express and middlewares
var express = require('express');
var expressBodyParser = require('body-parser');
const swaggerParser = require('swagger-parser');
var swStats = require('swagger-stats'); // require('swagger-stats');
// Mockup API implementation
var API = require('./api');
var app = module.exports = express();
app.use(expressBodyParser.json()); // for parsing application/json
app.use(expressBodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// all environments
app.set('port', process.env.PORT || 3040);
// Suppress cache on the GET API responses
app.disable('etag');
app.get('/', function(req,res) {
res.redirect('/swagger-stats/');
});
app.get('/apidoc.json', function(req,res){
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
var tlBucket = 60000;
if( process.env.SWS_TEST_TIMEBUCKET ){
tlBucket = parseInt(process.env.SWS_TEST_TIMEBUCKET);
}
const swaggerSpec = require('./swagger.json');
// Testing validation of 3rd-party API spec
const parser = new swaggerParser();
parser.validate(swaggerSpec,function(err, api) {
if (!err) {
debug('Success validating swagger file!');
swaggerSpec = api;
// Enable swagger-stats middleware
app.use(swStats.getMiddleware({
name: 'swagger-stats-testapp',
version: '0.95.19',
timelineBucketDuration: tlBucket,
uriPath: '/swagger-stats',
swaggerSpec:swaggerSpec,
elasticsearch: 'http://127.0.0.1:9200',
}));
// Implement custom API in application to return collected statistics
app.get('/stats', function(req,res){
res.setHeader('Content-Type', 'application/json');
res.send(swStats.getCoreStats());
});
// Connect API Router - it should be the end of the chain
app.use('/v2', API);
// Setup server
server = http.createServer(app);
server.listen(4005);
debug('Server started on port ' + app.get('port') + ' http://localhost:'+app.get('port'));
}
});
module.exports.app = app;