-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhandler.js
More file actions
50 lines (45 loc) · 1.55 KB
/
handler.js
File metadata and controls
50 lines (45 loc) · 1.55 KB
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
'use strict';
const { tickerGenerator } = require('./functions/ticker');
const { generate } = require('./functions/cmc-data-generator');
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
const _ = require('lodash');
const Logger = require('./functions/utils/logger');
const s3 = new AWS.S3();
const LOG_FILE = 'v1/log.txt';
module.exports.ticker = (event, context, callback) => {
tickerGenerator()
.then(({files, log}) => updateLog(log).then(() => files))
.then((files) => putFiles(files))
.then(() => generate())
.then((files) => putFiles(files))
.then(v => callback(null, v), callback);
};
function putFiles(files) {
return Promise.all(_.map(files, (contents, filename) => {
return s3.putObject({
Bucket: process.env.BUCKET,
Key: filename,
Body: contents,
ContentType: 'application/json',
ACL: 'public-read',
CacheControl: 'public, max-age=50',
}).promise()
}));
}
function updateLog(log) {
return s3.getObject({
Bucket: process.env.BUCKET,
Key: LOG_FILE
}).promise()
.then((data) => Logger.prepareLog(data.Body.toString('ascii') || ''))
.catch(() => '')
.then((currentLog) => s3.putObject({
Bucket: process.env.BUCKET,
Key: LOG_FILE,
Body: `${log}\n\n\n${currentLog}`.trim(),
ContentType: 'text/plain',
ACL: 'public-read',
CacheControl: 'public, max-age=50',
}).promise())
.catch()
}