All files / examples/testapp testapp.js

93.75% Statements 45/48
66.67% Branches 4/6
50% Functions 2/4
93.75% Lines 45/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137    1x 1x 1x     1x     1x 1x 1x 1x   1x 1x   1x     1x   1x 1x 1x 1x 1x 1x 1x 1x     1x     1x   1x       1x         1x 1x 1x       1x 1x 1x                                                                                     1x     1x   1x 1x 1x 1x     1x                   1x 1x 1x       1x     1x 1x 1x         1x  
'use strict';
 
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');
var expressFavicon = require('serve-favicon');
var expressStatic = require('serve-static');
 
var swaggerJSDoc = require('swagger-jsdoc');
var swaggerParser = require('swagger-parser');
 
var swStats = require('../../lib');    // require('swagger-stats');
 
// Mockup API implementation
var API = require('./api');
 
var app = module.exports = express();
app.use(expressFavicon(path.join(__dirname, '../../ui/favicon.png')));
app.use('/ui',expressStatic(path.join(__dirname, '../../ui')));
app.use('/ui/dist',expressStatic(path.join(__dirname, '../../dist')));
app.use('/node_modules',expressStatic(path.join(__dirname, '../../node_modules')));
app.use('/node_modules',expressStatic(path.join(__dirname, '../../node_modules')));
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('/ui');
});
 
app.get('/apidoc.json', function(req,res){
    res.setHeader('Content-Type', 'application/json');
    res.send(swaggerSpec);
});
 
var tlBucket = 60000;
Eif( process.env.SWS_TEST_TIMEBUCKET ){
    tlBucket = parseInt(process.env.SWS_TEST_TIMEBUCKET);
}
 
// SWAGGER-JSDOC Initialization //
var apifile = path.join(__dirname,'api.js');
debug('initializing swagger spec from api file %s',apifile);
var swOptions = {
    swaggerDefinition: {
        "info": {
            "description": "This is a Petstore API implementation for swagger-stats sample app",
            "version": "1.0.0",
            "title": "Swagger-Stats Petstore ",
            "contact": {
                "email": "sv2@slanatech.com"
            },
            "license": {
                "name": "MIT"
            }
        },
        "host": "localhost",
        "basePath": "/api/v1",
        "tags": [
            {
                "name": "pet",
                "description": "Everything about your Pets",
                "externalDocs": {
                    "description": "Find out more",
                    "url": "http://swaggerstats.io"
                }
            },
            {
                "name": "store",
                "description": "Access to Petstore orders"
            },
            {
                "name": "user",
                "description": "Operations about user",
                "externalDocs": {
                    "description": "Find out more about our store",
                    "url": "http://swaggerstats.io"
                }
            }
        ],
        "schemes": ["http"]
    },
    apis: [apifile]  // Path to the API files with swagger docs in comments
};
 
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
var swaggerSpec = swaggerJSDoc(swOptions);
 
// Testing validation of 3rd-party API spec
var parser = new swaggerParser();
 
parser.validate(swaggerSpec,function(err, api) {
    Eif (!err) {
        debug('Success validating swagger file!');
        swaggerSpec = api;
 
        // Enable swagger-stats middleware
        app.use(swStats.getMiddleware({
            name: 'swagger-stats-testapp',
            version: '0.95.15',
            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(app.get('port'));
        debug('Server started on port ' + app.get('port') + ' http://localhost:'+app.get('port'));
 
    }
});
 
module.exports.app = app;