-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
90 lines (79 loc) · 2.22 KB
/
handler.js
File metadata and controls
90 lines (79 loc) · 2.22 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
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
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
const ddbClient = new DynamoDBClient();
const docClient = DynamoDBDocumentClient.from(ddbClient);
const s3 = new S3Client();
export const redirect = async (event, context, callback) => {
const pathParams = event.pathParameters;
const noRedirect = {
statusCode: 200,
headers: noCacheHeaders(),
body: 'nothing to see here!'
};
if (!pathParams || !pathParams.id) {
return callback(null, redirectResponse('https://wpb.is/PatrickBrandt'));
}
const id = pathParams.id.toLowerCase();
try {
// do something special for yours truly - serve home site
if (id === 'patrickbrandt') {
callback(null, contentResponse(await getFileContent('index.html'), 'text/html'));
} else if (id === 'robots.txt') {
callback(null, contentResponse(await getFileContent(id), 'text/plain'));
} else {
const url = await getUrl(id);
if(url) {
return callback(null, redirectResponse(url));
}
callback(null, noRedirect);
}
} catch(err) {
console.log(err);
callback(null, errorResponse());
}
};
function contentResponse(content, contentType) {
return {
statusCode: 200,
headers: {
'Content-Type': contentType,
},
body: content,
};
}
async function getFileContent(fileName) {
const params = {
Bucket: process.env.wpbis_bucket,
Key: fileName,
};
const data = await s3.send(new GetObjectCommand(params));
return await data.Body.transformToString();
}
async function getUrl(id) {
const params = {
TableName: process.env.urls_table,
Key: { Id: id },
};
const data = await docClient.send(new GetCommand(params));
return data.Item ? data.Item.Url : undefined;
}
function errorResponse() {
return {
statusCode: 500,
body: 'something bad happened',
};
}
function redirectResponse(url) {
return {
statusCode: 301,
headers: { Location: url, ...noCacheHeaders()},
};
}
function noCacheHeaders() {
return {
'Cache-Control': 'no-cache, no-store, must-revalidate',
Pragma: 'no-cache',
Expires: 0,
};
}