-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·193 lines (169 loc) · 5.01 KB
/
server.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env node
'use strict';
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly)
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
} else {
obj[key] = value;
}
return obj;
}
var program = require('commander');
var path = require('path');
var _require = require('ramda'),
compose = _require.compose,
pluck = _require.pluck;
var cors = require('cors');
var express = require('express');
var debug = require('debug');
var logger = require('morgan-debug');
var Farso = require('.');
var loginfo = debug('farso');
var DEFAULT_CONFIG = './farso.config.js';
var listVibes = function listVibes(farso) {
return function (req, res) {
return res.send({
currentVibe: farso.currentVibe && farso.currentVibe.name,
vibes: compose(pluck('name'))(farso.listVibes()),
});
};
};
var selectVibe = function selectVibe(farso) {
return function (req, res) {
var vibe = req.params.vibe;
farso.select(vibe);
res.send({
currentVibe: farso.currentVibe.name,
});
};
};
var adminPath = '/_vibes_';
var initServer = function initServer(ctx) {
var _ctx$host = ctx.host,
host = _ctx$host === void 0 ? 'localhost' : _ctx$host,
port = ctx.port,
farso = ctx.farso,
bodySizeLimit = ctx.bodySizeLimit;
return new Promise(function (resolve, reject) {
var app = express();
app.use(cors());
app.use(
express.json({
limit: bodySizeLimit || '100kb',
}),
);
app.use(express.urlencoded());
app.get(''.concat(adminPath, '/:vibe'), selectVibe(farso));
app.get(adminPath, listVibes(farso));
app.use(logger('farso', 'dev'));
app.use(farso.router);
var srv = app.listen(port, host, function (err) {
if (err) return reject(err);
var _srv$address = srv.address(),
sport = _srv$address.port;
srv.url = 'http://'.concat(host, ':').concat(sport);
loginfo("server started on '".concat(srv.url, "'"));
loginfo("check vibes here: '".concat(srv.url).concat(adminPath, "'"));
return resolve(
_objectSpread(
_objectSpread({}, ctx),
{},
{
server: srv,
},
),
);
});
});
};
var initFarso = function initFarso(ctx) {
var endpoints = ctx.endpoints,
vibes = ctx.vibes,
globals = ctx.globals;
var farso = Farso({
endpoints: endpoints,
vibes: vibes,
globals: globals,
});
farso.on('mock.error', function (_ref) {
var message = _ref.message,
data = _ref.data;
loginfo(message);
console.log(data); // eslint-disable-line no-console
});
farso.on('endpoint.selected', function (vibe, endpoint) {
return loginfo("Endpoint '".concat(endpoint.getLabel(vibe), "' selected"));
});
farso.on('mock.visited', function (mock) {
return loginfo("Mock '".concat(mock.getLabel(), "' visited"));
});
farso.on('endpoint.added', function (endpoint) {
return loginfo("Endpoint '".concat(endpoint.getLabel(), "' created"));
});
farso.on('vibe.adding', function (vibe) {
return loginfo("Adding Vibe '".concat(vibe.name, "'"));
});
farso.on('vibe.updating', function (vibe) {
return loginfo("Updating Vibe '".concat(vibe.name, "'"));
});
farso.on('vibe.selected', function (vibe) {
return loginfo("Vibe '".concat(vibe.name, "' is now active"));
});
return Promise.resolve(
_objectSpread(
_objectSpread({}, ctx),
{},
{
farso: farso,
},
),
);
};
var runServer = function runServer(config) {
return initFarso(config)
.then(initServer)
.then(function (ctx) {
ctx.farso.loadConfig();
ctx.farso.start();
return ctx;
});
};
if (require.main === module) {
program.option('-c, --config <path>', 'set config path').parse(process.argv);
var config = require(path.join(process.cwd(), program.config || DEFAULT_CONFIG));
runServer(config)['catch'](console.error); // eslint-disable-line no-console
}
module.exports = {
runServer: runServer,
initFarso: initFarso,
initServer: initServer,
};