Skip to content

Commit 3c54b63

Browse files
committed
Update server and lambda js files to only onvoke via AWS SDK
1 parent 5e66db6 commit 3c54b63

File tree

3 files changed

+58
-97
lines changed

3 files changed

+58
-97
lines changed
Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
1-
const awsServerlessExpress = require('aws-serverless-express');
2-
const { app } = require('./server');
1+
const { extractAndParseGDB } = require('./server');
32

4-
const binaryMimeTypes = [
5-
'multipart/form-data',
6-
'application/zip',
7-
'application/octet-stream'
8-
];
9-
10-
const server = awsServerlessExpress.createServer(app, null, binaryMimeTypes);
11-
12-
exports.handler = (event, context) => {
3+
exports.handler = async (event) => {
134
console.log("=== Incoming Lambda Event ===");
14-
console.log("HTTP Method:", event.requestContext?.http?.method);
15-
console.log("Raw Path:", event.rawPath);
16-
console.log("Headers:", JSON.stringify(event.headers, null, 2));
17-
console.log("Is Base64 Encoded:", event.isBase64Encoded);
18-
console.log("Body (truncated):", event.body?.slice(0, 300));
19-
console.log("============================");
5+
console.log("Event payload:", JSON.stringify(event, null, 2).slice(0, 500) + "...");
6+
7+
try {
8+
// Check if we have the file data in the expected field
9+
const base64Zip = event.file;
10+
if (!base64Zip) {
11+
console.error("No file data found in the event");
12+
return {
13+
statusCode: 400,
14+
body: JSON.stringify({ error: "Missing 'file' in payload" }),
15+
headers: { "Content-Type": "application/json" }
16+
};
17+
}
2018

21-
// Map the HTTP API v2 event to something Express can understand
22-
if (event.requestContext && event.requestContext.http) {
23-
// For HTTP API v2
24-
event.httpMethod = event.requestContext.http.method;
25-
event.path = event.rawPath;
19+
// Convert base64 to buffer and process it
20+
const buffer = Buffer.from(base64Zip, "base64");
21+
console.log(`Received file size: ${buffer.length} bytes`);
2622

27-
// Extract client IP address for rate limiter
28-
if (event.headers && (event.headers['x-forwarded-for'] || event.headers['X-Forwarded-For'])) {
29-
// Normalize header name (API Gateway might use different casing)
30-
const forwardedHeader = event.headers['x-forwarded-for'] || event.headers['X-Forwarded-For'];
31-
// Set the IP for express-rate-limit
32-
event.ip = forwardedHeader.split(',')[0].trim();
33-
} else if (event.requestContext.http.sourceIp) {
34-
// Fallback to source IP
35-
event.ip = event.requestContext.http.sourceIp;
36-
}
37-
}
23+
// Process the file
24+
const results = await extractAndParseGDB(buffer);
25+
console.log(`Extracted ${results.length} geometries from GDB`);
3826

39-
return awsServerlessExpress.proxy(server, event, context);
27+
// Return the results
28+
return {
29+
statusCode: 200,
30+
body: JSON.stringify(results),
31+
headers: { "Content-Type": "application/json" }
32+
};
33+
} catch (err) {
34+
console.error("Lambda handler error:", err);
35+
return {
36+
statusCode: 500,
37+
body: JSON.stringify({
38+
error: "Failed to process GDB",
39+
message: err.message
40+
}),
41+
headers: { "Content-Type": "application/json" }
42+
};
43+
}
4044
};

node/wfprev-gdb-extractor/server.js

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,17 @@ const fs = require("fs");
55
const path = require("path");
66
const extract = require("extract-zip");
77
const rateLimit = require("express-rate-limit");
8-
const awsServerlessExpressMiddleware = require("aws-serverless-express/middleware");
98

109
const app = express();
1110
app.set("trust proxy", 1);
1211
app.disable("x-powered-by");
1312

14-
app.use(awsServerlessExpressMiddleware.eventContext());
15-
1613
const limiter = rateLimit({
1714
windowMs: 15 * 60 * 1000, // 15 minutes
18-
max: 100,
19-
// Add a custom key generator that handles Lambda events
20-
keyGenerator: (req) => {
21-
// Check for IP set by Lambda handler
22-
if (req.ip) return req.ip;
23-
24-
// Fallback to Express standard methods
25-
return req.ip ||
26-
(req.headers && (req.headers['x-forwarded-for'] || req.headers['X-Forwarded-For'])) ||
27-
req.connection.remoteAddress ||
28-
'127.0.0.1';
29-
},
30-
// Skip check for health endpoints if you have any
31-
skip: (req) => {
32-
return req.path === '/health' || req.path === '/ping';
33-
},
34-
// Handle errors more gracefully
35-
handler: (req, res, next, options) => {
36-
console.log("Rate limit exceeded:", req.ip);
37-
res.status(options.statusCode).send("Too many requests, please try again later.");
38-
}
15+
max: 100
3916
});
4017
app.use(limiter);
4118

42-
app.use((req, res, next) => {
43-
const event = req.apiGateway?.event;
44-
if (event?.isBase64Encoded && event.body) {
45-
const buff = Buffer.from(event.body, "base64");
46-
req.body = buff;
47-
req.headers["content-length"] = buff.length;
48-
}
49-
next();
50-
});
51-
5219
app.use(fileUpload());
5320

5421
const uploadDir = "/tmp/uploads";
@@ -65,8 +32,15 @@ async function extractAndParseGDB(zipBuffer, zipFileName = "upload.zip") {
6532
const zipPath = path.join(uploadDir, zipFileName);
6633
const unzipPath = path.join(uploadDir, path.basename(zipFileName, ".zip"));
6734

35+
// Ensure upload directory exists
36+
if (!fs.existsSync(uploadDir)) {
37+
fs.mkdirSync(uploadDir, { recursive: true });
38+
}
39+
40+
// Write the buffer to a file
6841
fs.writeFileSync(zipPath, zipBuffer);
6942

43+
// Extract the zip file
7044
await extract(zipPath, {
7145
dir: unzipPath,
7246
onEntry: (entry) => {
@@ -77,10 +51,12 @@ async function extractAndParseGDB(zipBuffer, zipFileName = "upload.zip") {
7751
}
7852
});
7953

54+
// Find the .gdb folder
8055
const files = fs.readdirSync(unzipPath);
8156
const gdbFolder = files.find(f => f.endsWith(".gdb"));
8257
if (!gdbFolder) throw new Error("No .gdb found.");
8358

59+
// Parse the GDB
8460
const gdbPath = path.join(unzipPath, gdbFolder);
8561
const dataset = gdal.open(gdbPath);
8662
const results = [];
@@ -111,7 +87,9 @@ async function extractAndParseGDB(zipBuffer, zipFileName = "upload.zip") {
11187
}
11288
};
11389
deleteRecursive(unzipPath);
114-
} catch (_) {}
90+
} catch (err) {
91+
console.error("Cleanup error:", err);
92+
}
11593

11694
return results;
11795
}
@@ -144,31 +122,5 @@ if (require.main === module) {
144122
module.exports = {
145123
app,
146124
handleUpload,
147-
};
148-
149-
exports.handler = async (event) => {
150-
try {
151-
const base64Zip = event.file;
152-
if (!base64Zip) {
153-
return {
154-
statusCode: 400,
155-
body: "Missing 'file' in payload"
156-
};
157-
}
158-
159-
const buffer = Buffer.from(base64Zip, "base64");
160-
const results = await extractAndParseGDB(buffer);
161-
162-
return {
163-
statusCode: 200,
164-
body: JSON.stringify(results),
165-
headers: { "Content-Type": "application/json" }
166-
};
167-
} catch (err) {
168-
console.error("Lambda handler error:", err);
169-
return {
170-
statusCode: 500,
171-
body: "Failed to process GDB: " + err.message
172-
};
173-
}
174-
};
125+
extractAndParseGDB // Export this function to be used by the Lambda handler
126+
};

terraform/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ variable "WFPREV_BASE_URL" {
121121
default = ""
122122
}
123123

124+
variable "WFPREV_GDB_FUNCTION_NAME" {
125+
type = string
126+
default = ""
127+
}
128+
124129
variable "WFDM_BASE_URL" {
125130
type = string
126131
default = ""

0 commit comments

Comments
 (0)