Repository navigation
Expand file tree
/
Copy pathapp.ts
More file actions
112 lines (97 loc) · 3.29 KB
/
Copy pathapp.ts
File metadata and controls
112 lines (97 loc) · 3.29 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { PrismaClient } from '../prisma/generated/client';
import { RouteConfig, Routes } from './utils/types';
import { composeMiddleware } from './utils/middleware';
import { getDatabaseUrl } from './utils/secrets';
import { healthRoutes } from './routes/health';
import { webRoutes } from './routes/web';
import { apiRoutes } from './routes/api';
let prisma: PrismaClient | undefined;
async function getPrismaClient(): Promise<PrismaClient> {
if (!prisma) {
const databaseUrl = await getDatabaseUrl();
prisma = new PrismaClient({
datasources: {
db: {
url: databaseUrl,
},
},
});
}
return prisma;
}
const routes: Routes = {
...healthRoutes,
...webRoutes,
...apiRoutes,
};
const route = (method: string, path: string): RouteConfig | void => {
// Fast lookup for static routes
if (routes[path] && routes[path][method] && !routes[path][method].patternMatching) {
return routes[path][method];
}
// Check for dynamic routes like /v1/chart/{chartHash}/play
for (const routePath in routes) {
const routeConfig = routes[routePath];
if (routeConfig[method]) {
const patternMatching = routeConfig[method].patternMatching;
if (patternMatching) {
// Replace path parameters with regex patterns
let regexPath = routePath;
for (const param in patternMatching) {
const regex = patternMatching[param];
// Use a named capture group to extract parameters
regexPath = regexPath.replace(`{${param}}`, `(?<${param}>${regex.source})`);
}
// Test the path against the regex
const regex = new RegExp(`^${regexPath}$`);
if (regex.test(path)) {
// Extract path parameters if they exist
const matches = path.match(regex);
return {
...routeConfig[method],
patternMatching: routeConfig[method].patternMatching,
routeParameters: matches ? matches.groups : {},
};
}
}
}
}
return;
};
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const path = event.path;
const method = event.httpMethod;
console.log(`Received request: ${method} ${path}`);
// Handle CORS preflight requests
if (method === 'OPTIONS') {
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',
},
body: '',
};
}
const routeConfig = route(method, path);
if (routeConfig) {
const extendedEvent = {
...event,
routeParameters: routeConfig.routeParameters || {},
};
const prismaClient = await getPrismaClient();
const composedHandler = composeMiddleware(routeConfig, prismaClient);
return await composedHandler(extendedEvent);
}
return {
statusCode: 404,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',
},
body: '',
};
};