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