forked from binuks/tachyon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-api-gateway.js
49 lines (45 loc) · 1.41 KB
/
local-api-gateway.js
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
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const {
spawn
} = require('child_process');
http.createServer(function (request, response) {
const bucket = 'hmn-uploads-eu';
const region = 'eu-west-1';
const task = __dirname;
const pathName = url.parse(request.url).pathname;
const query = url.parse(request.url).query;
const args = {
path: pathName,
queryStringParameters: querystring.parse( query ),
headers: request.headers,
};
const childArgs = ['run', '--rm', '-e', `S3_BUCKET=${ bucket }`, '-e', `S3_REGION=${ region }`, '-v', `${ task }:/var/task`, 'public.ecr.aws/lambda/nodejs:14', 'lambda-handler.handler', JSON.stringify(args)];
const child = spawn('docker', childArgs);
var stdout = '';
child.stdout.on('data', data => stdout += data);
child.stderr.on('data', data => console.warn(String(data)));
child.on('close', function () {
let lambdaExec;
try {
lambdaExec = JSON.parse(stdout);
} catch ( e ) {
console.error( e );
return;
}
if ( lambdaExec.errorMessage ) {
response.writeHead( 500 );
response.write(JSON.stringify( lambdaExec ) );
response.end();
return;
}
response.writeHead(lambdaExec.statusCode, lambdaExec.headers);
if ( lambdaExec.isBase64Encoded ) {
response.write(Buffer.from(lambdaExec.body, 'base64'));
} else {
response.write(lambdaExec.body);
}
response.end();
});
}).listen(7000);