-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
89 lines (84 loc) · 2.46 KB
/
index.ts
File metadata and controls
89 lines (84 loc) · 2.46 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
import { APIGatewayProxyEventV2 } from "aws-lambda";
import { APIGatewayProxyStructuredResultV2 } from "aws-lambda/trigger/api-gateway-proxy";
import { MeetupEvent } from "./dao/meetup.dao";
import { AppConf } from "./app-conf";
import { Controller, routes } from "./routes";
import colors from "colors";
export type EventsResponse = Array<MeetupEvent>;
export async function handler(
event: APIGatewayProxyEventV2
): Promise<APIGatewayProxyStructuredResultV2> {
try {
return await handleRequest(event);
} catch (e) {
console.error(`Internal server error: ${e}`);
return {
statusCode: 500,
body: JSON.stringify({
message: "Internal server error",
requestId: event.requestContext.requestId,
}),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
};
}
}
async function handleRequest(
event: APIGatewayProxyEventV2
): Promise<APIGatewayProxyStructuredResultV2> {
console.log("request received");
if (!isApiKeyValid(event)) {
return {
statusCode: 401,
body: JSON.stringify({
message: "Unauthorized",
}),
headers: {
"Content-Type": "application/json",
},
};
}
const path = event.requestContext.http.path;
const method = event.requestContext.http.method.toUpperCase();
let controller = undefined as undefined | Controller;
for (const route of routes) {
if (method === route.method && new RegExp(`^${route.path}$`).test(path)) {
controller = route.controller;
break;
}
}
if (controller) {
const response = await controller(event);
if (!response.headers) {
response.headers = {};
}
response.headers["Access-Control-Allow-Origin"] = "*";
return response;
}
console.log(
colors.blue("No controller found for path ") + colors.yellow(`"${path}"`)
);
return {
statusCode: 404,
body: JSON.stringify({
message: "Not found",
requestId: event.requestContext.requestId,
}),
headers: {
"Content-Type": "application/json",
},
};
}
const API_KEY_PATH = /^\/api\/.*/;
/**
* Checks if an API key is needed, and if so, if it is valid. API Keys are required for all non cached requests.
* @param request The request to validate.
*/
function isApiKeyValid(request: APIGatewayProxyEventV2): boolean {
if (API_KEY_PATH.test(request.requestContext.http.path)) {
return request.headers?.["x-api-key"] === AppConf.apiKey;
}
return true;
}