-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
178 lines (164 loc) · 6.39 KB
/
index.js
File metadata and controls
178 lines (164 loc) · 6.39 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import crypto from 'crypto';
import functions from '@google-cloud/functions-framework';
// Dynamic imports for better performance - only load when needed
const controllers = {
technologies: null,
categories: null,
adoption: null,
cwvtech: null,
lighthouse: null,
pageWeight: null,
audits: null,
ranks: null,
geos: null,
versions: null,
geoBreakdown: null,
static: null
};
// Helper function to dynamically import controllers
const getController = async (name) => {
if (!controllers[name]) {
switch (name) {
case 'technologies':
controllers[name] = await import('./controllers/technologiesController.js');
break;
case 'categories':
controllers[name] = await import('./controllers/categoriesController.js');
break;
case 'adoption':
case 'cwvtech':
case 'lighthouse':
case 'pageWeight':
case 'audits':
controllers[name] = await import('./controllers/reportController.js');
break;
case 'ranks':
controllers[name] = await import('./controllers/ranksController.js');
break;
case 'geos':
controllers[name] = await import('./controllers/geosController.js');
break;
case 'versions':
controllers[name] = await import('./controllers/versionsController.js');
break;
case 'geoBreakdown':
controllers[name] = await import('./controllers/geoBreakdownController.js');
break;
case 'static':
controllers[name] = await import('./controllers/cdnController.js');
break;
}
}
return controllers[name];
};
// Helper function to set CORS headers
const setCORSHeaders = (res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Accept, If-None-Match');
res.setHeader('Access-Control-Expose-Headers', '*');
res.setHeader('Access-Control-Max-Age', '86400');
};
// Helper function to set common response headers
const setCommonHeaders = (res) => {
setCORSHeaders(res);
res.setHeader('Content-Type', 'application/json');
// Browser cache: 1 hour, CDN cache: 30 days
res.setHeader('Cache-Control', 'public, max-age=3600, s-maxage=2592000');
res.setHeader('Cloud-CDN-Cache-Tag', 'report-api');
res.setHeader('Timing-Allow-Origin', '*');
};
// Helper function to generate ETag
const generateETag = (jsonData) => {
return crypto.createHash('md5').update(jsonData).digest('hex');
};
// Helper function to send JSON response with ETag support
const sendJSONResponse = (res, data, statusCode = 200) => {
const jsonData = JSON.stringify(data);
const etag = generateETag(jsonData);
res.setHeader('ETag', `"${etag}"`);
res.statusCode = statusCode;
res.end(jsonData);
};
// Helper function to check if resource is modified
const isModified = (req, etag) => {
const ifNoneMatch = req.headers['if-none-match'] || (req.get && req.get('if-none-match'));
return !ifNoneMatch || ifNoneMatch !== `"${etag}"`;
};
// Route handler function
const handleRequest = async (req, res) => {
try {
setCommonHeaders(res);
// Handle OPTIONS requests for CORS preflight
if (req.method === 'OPTIONS') {
res.statusCode = 204;
res.end();
return;
}
// Parse URL path - robustly handle Express (req.path) or native Node (req.url)
const pathname = req.path || req.url.split('?')[0];
// Route handling
if (pathname === '/' && req.method === 'GET') {
// Health check endpoint
const data = { status: 'ok' };
sendJSONResponse(res, data);
} else if (pathname === '/v1/technologies' && req.method === 'GET') {
const { listTechnologies } = await getController('technologies');
await listTechnologies(req, res);
} else if (pathname === '/v1/categories' && req.method === 'GET') {
const { listCategories } = await getController('categories');
await listCategories(req, res);
} else if (pathname === '/v1/adoption' && req.method === 'GET') {
const { listAdoptionData } = await getController('adoption');
await listAdoptionData(req, res);
} else if (pathname === '/v1/cwv' && req.method === 'GET') {
const { listCWVTechData } = await getController('cwvtech');
await listCWVTechData(req, res);
} else if (pathname === '/v1/lighthouse' && req.method === 'GET') {
const { listLighthouseData } = await getController('lighthouse');
await listLighthouseData(req, res);
} else if (pathname === '/v1/page-weight' && req.method === 'GET') {
const { listPageWeightData } = await getController('pageWeight');
await listPageWeightData(req, res);
} else if (pathname === '/v1/audits' && req.method === 'GET') {
const { listAuditsData } = await getController('audits');
await listAuditsData(req, res);
} else if (pathname === '/v1/ranks' && req.method === 'GET') {
const { listRanks } = await getController('ranks');
await listRanks(req, res);
} else if (pathname === '/v1/geos' && req.method === 'GET') {
const { listGeos } = await getController('geos');
await listGeos(req, res);
} else if (pathname === '/v1/versions' && req.method === 'GET') {
const { listVersions } = await getController('versions');
await listVersions(req, res);
} else if (pathname === '/v1/geo-breakdown' && req.method === 'GET') {
const { listGeoBreakdownData } = await getController('geoBreakdown');
await listGeoBreakdownData(req, res);
} else if (pathname.startsWith('/v1/static/') && req.method === 'GET') {
// GCS proxy endpoint for reports files
const filePath = decodeURIComponent(pathname.replace('/v1/static/', ''));
if (!filePath) {
res.statusCode = 400;
res.end(JSON.stringify({ error: 'File path required' }));
return;
}
const { proxyReportsFile } = await getController('static');
await proxyReportsFile(req, res, filePath);
} else {
// 404 Not Found
res.statusCode = 404;
res.end(JSON.stringify({ error: 'Not Found' }));
}
} catch (error) {
console.error('Error:', error);
res.statusCode = 400;
res.end(JSON.stringify({
errors: [{ error: error.message || 'Unknown error occurred' }]
}));
}
};
// Register with Functions Framework
functions.http('app', handleRequest);
// Export for testing using Functions Framework testing utilities
export { handleRequest as app };