-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.js
104 lines (93 loc) · 2.78 KB
/
handler.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
import apiClient from './get/posts';
import webClient from './src/server';
/*
BOILERPLATE STARTS HERE
Usually you don't have to touch anything below this.
(unless you are using this for actual production app and need to use Cognito & SNS & such)
*/
export function webapp(event, context, callback) {
return Promise.resolve()
.then(() => {
if (event.httpMethod && event.resource) {
console.log('PROCESSING WEBAPP REQUEST', event.httpMethod, event.resource);
if (event.path === '/favicon.ico') {
callback(null, {});
return;
}
webClient(event)
.then((body) => {
callback(
null,
{
statusCode: 200,
headers: {
'content-type': 'text/html; charset=utf-8',
},
body,
},
);
});
}
});
}
export function staticFile(event, context, callback) {
// This is a proxy for static files (i.e. bundled React app)
// This is only used in `sls offline`
// TODO: Disable in production build
const fs = require('fs'); // eslint-disable-line global-require
const mime = require('mime-types'); // eslint-disable-line global-require
const data = fs.readFileSync(`./build${event.path}`, 'base64');
const contentType = mime.lookup(`./build${event.path}`);
callback(
null,
{
statusCode: 200,
isBase64Encoded: true,
headers: {
'Content-Type': contentType,
},
body: data,
},
);
}
export function request(event, context, callback) {
return Promise.resolve()
.then(() => {
if (event.httpMethod && event.path) {
console.log('PROCESSING HTTP EVENT', event.httpMethod, event.path);
return apiClient(event);
}
console.log('UNKNOWN EVENT', event);
return {};
})
.then(sendProxySuccess.bind(null, callback), sendProxyError.bind(null, callback)); // eslint-disable-line
}
function sendProxySuccess(callback, responseObj) {
const response = responseObj && responseObj.statusCode ? responseObj : {
statusCode: 200,
body: JSON.stringify(responseObj),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
callback(null, response);
}
function sendProxyError(callback, err) {
console.log('ERROR:', err.stack || err);
let status = 500;
let message = err.message || JSON.stringify(err);
const m = err.message && err.message.match(/^\[(\d+)\] *(.*)$/);
if (m) {
[, status, message] = m;
}
const response = {
statusCode: status,
body: JSON.stringify({ errorMessage: message }),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
};
callback(null, response);
}