UNPKG

2.5 kBJavaScriptView Raw
1'use strict';
2
3var _ = require('./');
4
5var _2 = _interopRequireDefault(_);
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9var express = require('express');
10var http = require('http');
11
12// see http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html
13// run dynamo with java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
14// var dynamo = new AWS.DynamoDB()
15// dynamo.setEndpoint('http://localhost:8000')
16
17_2.default.prototype.serverStart = function () {
18 var _this = this;
19
20 var port = arguments.length <= 0 || arguments[0] === undefined ? 3333 : arguments[0];
21
22 var app = express();
23
24 app.use(function (req, res, next) {
25 _this.readConfig().then(function (config) {
26 var path = req.path.split('/');
27
28 var endpoint = config.api.endpoints.find(function (endpoint) {
29 if (endpoint.method !== req.method) return false;
30 var comps = endpoint.path.split('/');
31
32 if (comps.length !== path.length) return false;
33 endpoint.params = {};
34 var matches = path.every(function (comp) {
35 var c = comps.shift();
36 if (c.substring(0, 1) === '{' && c.substring(c.length - 2, 1)) {
37 endpoint.params[c.substring(1, c.length - 1)] = comp;
38 return true;
39 }
40 return c === comp;
41 });
42
43 return matches;
44 });
45
46 if (!endpoint) {
47 return res.status(404).json({ message: 'Not found' });
48 }
49
50 var func = _this._findFunction(config, { functionName: endpoint.functionName });
51 if (!func) {
52 return res.status(500).json({
53 message: 'Lambda function ' + endpoint.functionName + ' not found'
54 });
55 }
56
57 var event = {
58 header: req.headers,
59 querystring: req.query,
60 body: req.body,
61 path: endpoint.params
62 };
63 var context = {
64 succeed: function succeed(output) {
65 if (endpoint.output === 'html') {
66 res.contentType('text/html');
67 res.send(output.html);
68 } else {
69 res.json(output);
70 }
71 }
72 };
73 var controller = require(_this._fullpath(func.filename));
74 controller[func.handler](event, context);
75 }).catch(function (e) {
76 return res.status(500).json({
77 message: e.message || 'Internal error'
78 });
79 });
80 });
81 http.createServer(app).listen(port);
82
83 return app;
84};
85//# sourceMappingURL=server.js.map
\No newline at end of file