-
Notifications
You must be signed in to change notification settings - Fork 30
/
ramlServer.js
66 lines (53 loc) · 1.5 KB
/
ramlServer.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
#!/usr/bin/env node
// RAML Server 0.1.1
// (c) 2015 Franco Arolfo
// RAML Server may be freely distributed under the MIT license.
// For all details and documentation:
// https://github.com/farolfo/raml-server
var ramlMocker = require('raml-mocker'),
_ = require('underscore'),
jsonServer = require('json-server'),
argv = require('yargs').argv;;
var options = {
path: argv.path || '.'
},
mocksAmount = 10,
port = process.env.PORT || 3000,
server,
router;
function reqToDBEntry(request) {
var resource = request.uri.substr(1),
obj = {};
var response = {};
if ( request.example != undefined && request.example() != undefined ) {
response = JSON.parse( request.example() );
} else if ( request.mock != undefined ) {
response = request.mock();
} else {
response = {};
}
obj[resource] = response;
return obj;
}
function asList(func, n) {
if (n <= 0) {
return [];
}
var ans = [];
ans.push(func());
return ans.concat(asList(func, n-1));
}
function generateServer(db) {
server = jsonServer.create();
router = jsonServer.router(db);
server.use(jsonServer.defaults);
server.use(router);
console.log('Running RAML server on localhost:' + port + '...');
server.listen(port);
}
ramlMocker.generate(options, function (requestsToMock) {
generateServer(_.reduce(_.map(requestsToMock, reqToDBEntry), _.extend, {}));
});
process.on('SIGTERM', function() {
process.exit();
});