/
index.js
54 lines (46 loc) · 1.34 KB
/
index.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
"use strict";
const pulumi = require("@pulumi/pulumi");
const cloud = require("@pulumi/cloud");
const bucket = new cloud.Bucket("texy-outputs");
const bucketName = bucket.bucket.id;
const texy = new cloud.Task("texy", {
build: "./lambdas/texy",
memoryReservation: 512
});
const texyApi = new cloud.API("texy-api");
texyApi.post("/", async (req, res) => {
const crypto = require("crypto");
const hash = crypto.createHash("sha256");
hash.update(req.body.toString());
const inputFile = `${hash.digest("hex").substr(0, 12)}.texy`;
const outputFile = inputFile.replace(".texy", ".html");
console.log(`uploading file ${inputFile} to S3 bucket ${bucketName.get()}`);
try {
await bucket.put(inputFile, req.body);
console.log("OK");
res.status(200).json({
input: `s3.//${bucketName.get()}/${inputFile}`,
output: `s3://${bucketName.get()}/${outputFile}`
});
} catch (ex) {
console.error(ex);
res.status(500).write(ex.message);
}
});
bucket.onPut(
"newTexyFile",
async args => {
const file = args.key;
const result = file.replace(".texy", ".html");
texy.run({
environment: {
INPUT_FILE: file,
S3_BUCKET: bucketName.get(),
OUTPUT_FILE: result
}
});
},
{ keySuffix: ".texy" }
);
exports.apiEndpoint = texyApi.publish().url;
exports.bucketName = bucketName;