-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.ts
150 lines (125 loc) · 3.52 KB
/
handler.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { S3, DynamoDB } from 'aws-sdk';
import adjectives from './adjectives';
import felines from './felines';
import * as fs from 'fs';
import * as path from 'path';
const dynamoDB = new DynamoDB.DocumentClient();
const s3 = new S3();
const randStringFromList = (list: string[]): string => {
const len = list.length;
const randIndex = Math.floor(Math.random() * len);
const randString = list[randIndex];
return randString;
};
const generateCatrl = (): string => {
const firstAdj = randStringFromList(adjectives);
const secondAdj = randStringFromList(adjectives);
const feline = randStringFromList(felines);
const catrl = `${firstAdj}${secondAdj}${feline}`;
return catrl;
};
const isObjEmpty = (obj: object): boolean => {
return Object.keys(obj).length === 0 && obj.constructor === Object;
};
export const newCatrl = async (event) => {
let response = {
statusCode: 0,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: '',
};
if (
!event.queryStringParameters ||
!event.queryStringParameters.url ||
event.queryStringParameters.url === ''
) {
response.statusCode = 500;
response.body = JSON.stringify({ error: 'Invalid URL.' });
return response;
}
const urlRegExp: RegExp = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/;
if (!urlRegExp.test(event.queryStringParameters.url)) {
response.statusCode = 500;
response.body = JSON.stringify({ error: 'Invalid URL.' });
return response;
}
let catrl: string = generateCatrl();
const getParams = {
TableName: process.env.DYNAMODB_TABLE,
Key: {
catrl,
},
};
try {
let result = (await dynamoDB.get(getParams).promise()) as any;
while (!isObjEmpty(result)) {
catrl = generateCatrl();
getParams.Key.catrl = catrl;
result = (await dynamoDB.get(getParams).promise()) as any;
}
const putParams = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
catrl,
url: event.queryStringParameters.url,
},
};
await dynamoDB.put(putParams).promise();
response.statusCode = 200;
response.body = JSON.stringify({ result: putParams.Item.catrl });
} catch (error) {
response.statusCode = 500;
response.body = JSON.stringify({ error });
console.error(error);
}
return response;
};
export const getURL = async (event) => {
let response: any = {
statusCode: 0,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: '',
};
let params = {
TableName: process.env.DYNAMODB_TABLE,
Key: {
catrl: event.pathParameters.catrl,
},
};
try {
const result = (await dynamoDB.get(params).promise()) as any;
if (isObjEmpty(result)) {
response.statusCode = 404;
response.body = JSON.stringify({ error: 'Invalid catrl' });
} else {
response.statusCode = 301;
response.headers = {
...response.headers,
Location: result.Item.url,
};
response.body = JSON.stringify({
result: result.Item.url,
});
}
} catch (error) {
response.statusCode = 500;
response.body = JSON.stringify({ error });
console.error(error);
}
return response;
};
export const getStatic = async (event) => {
const html = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf8');
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': '*',
},
body: html,
};
return response;
};