-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.js
73 lines (59 loc) · 1.4 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
import zlib from 'zlib';
import AWS from 'aws-sdk';
import renderPath from '../app/libs/server/renderPath';
const s3 = new AWS.S3({
apiVersion: '2006-03-01'
});
const baseTemplateContext = {
title: 'Resume',
jsAsset: __JS_ASSET__,
cssAsset: __CSS_ASSET__
};
function putObject(params) {
return new Promise((resolve, reject) => {
s3.putObject(params, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
function getSNSMessage(event) {
const records = event.Records;
if (records && records.length) {
const firstRecord = records[0];
if (firstRecord.Sns) {
return JSON.parse(firstRecord.Sns.Message);
}
}
return {};
}
export const handler = (event, context, cb) => {
const message = getSNSMessage(event);
const templateContext = {
...baseTemplateContext,
generated: Date.now().toString()
};
console.log('Message', JSON.stringify(message));
if (message) {
templateContext.revision = message.after;
}
renderPath('/', templateContext)
.then(html => {
const gzipped = zlib.gzipSync(html);
return putObject({
Bucket: 'andrew.sprou.se',
Key: 'index.html',
Body: gzipped,
ACL: 'public-read',
ContentType: 'text/html',
ContentEncoding: 'gzip'
});
})
.then(cb)
.catch((error) => {
cb(JSON.stringify(error));
});
};