-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (76 loc) · 2.33 KB
/
index.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
81
82
83
84
85
86
87
88
89
var express = require('express');
var app = express();
var fs = require('fs');
var request = require('superagent');
var async = require('async');
var _ = require('lodash');
var swaggerFormatter = require('./app/swagger-formatter');
// heroku
app.set('port', (process.env.PORT || 3000));
// openshift
//var port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
//var ip = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
app.use(express.static(__dirname + '/app'));
app.get('/spec', function(req, res) {
/*
fs.readFile('app/sample.swagger.json', 'utf8', function(err, data) {
if(err) throw err;
res.json(JSON.parse(data));
});
*/
res.json("{}");
});
app.get('/spec/:masterKey/:apiKey', function(req, res) {
var baseUrl = 'http://api.everlive.com/v1/Metadata/Applications/';
var apiKey = req.params.apiKey,
masterKey = req.params.masterKey,
authorization = 'Masterkey ' + masterKey;
//console.log('starting');
async.waterfall([
function loadTypes(callback) {
request
.get(baseUrl + apiKey + '/Types')
.set('Authorization', authorization)
.set('Accept', 'application/json')
.end(function(err, typesResponse) {
if(err) {
return res.send(err);
}
//console.log('loaded types');
callback(null, typesResponse.body.Result);
});
},
function loadFields(types, callback) {
var fullTypes = [];
//console.log('loading fields');
async.each(types, function(type, cb) {
var clonedType = _.cloneDeep(type);
request
.get(baseUrl + apiKey + '/Types/' + type.Id + '/Fields')
.set('Authorization', authorization)
.set('Accept', 'application/json')
.end(function(err, fieldsResponse) {
if(err) return cb(err);
clonedType.fields = fieldsResponse.body.Result;
fullTypes.push(clonedType);
cb();
});
}, function(err) {
if(err) return callback(err);
callback(null, fullTypes);
});
}, function formatSwagger(types, callback) {
var swaggerFormat = swaggerFormatter.format(types, apiKey);
callback(null, swaggerFormat);
}
], function(err, swaggerFormat) {
if(err) return res.status(400).send(err);
res.json(swaggerFormat);
});
});
// openshift
//app.listen(port, ip, function() {
// heroku
app.listen(app.get('port'), function() {
console.log("Node server running at http://localhost:" + app.get('port'));
});