forked from stackery/php-lambda-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap
More file actions
executable file
·92 lines (83 loc) · 2.96 KB
/
Copy pathbootstrap
File metadata and controls
executable file
·92 lines (83 loc) · 2.96 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
#!/opt/bin/php -c/opt/php.ini
<?php
error_reporting(E_ALL | E_STRICT);
$AWS_LAMBDA_RUNTIME_API = getenv('AWS_LAMBDA_RUNTIME_API');
$HTTP_METHOD = 'POST';
function start_webserver() {
$pid = pcntl_fork();
switch($pid) {
case -1:
die('Failed to fork webserver process');
case 0:
$HANDLER = getenv('_HANDLER');
$HANDLER_ARRAY = explode('.', $HANDLER);
$handler_function = $HANDLER_ARRAY["1"];
$handler_components = explode('/', $HANDLER_ARRAY["0"]);
$handler_path = implode('/', array_merge(['/var/task'], $handler_components));
$handler_path .= ".php";
putenv("handler_function=$handler_function");
putenv("handler_path=$handler_path");
exec("PHP_INI_SCAN_DIR=/opt/etc/php-7.3.d/:/var/task/php-7.3.d/ php -S localhost:8000 -c /var/task/php.ini -d extension_dir=/opt/lib/php/7.3/modules '/opt/init.php'");
exit;
// return the child pid to parent
default:
// Wait for child server to start
sleep(1);
return $pid;
}
}
start_webserver();
while (true) {
$ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/next");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$invocation_id = '';
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$invocation_id) {
if (!preg_match('/:\s*/', $header)) {
return strlen($header);
}
[$name, $value] = preg_split('/:\s*/', $header, 2);
if (strtolower($name) == 'lambda-runtime-aws-request-id') {
$invocation_id = trim($value);
}
return strlen($header);
});
$body = '';
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$body) {
$body .= $chunk;
return strlen($chunk);
});
curl_exec($ch);
if (curl_error($ch)) {
die('Failed to fetch next Lambda invocation: ' . curl_error($ch) . "\n");
}
if ($invocation_id == '') {
die('Failed to determine Lambda invocation ID');
}
curl_close($ch);
$ch = curl_init("http://localhost:8000");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $HTTP_METHOD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
curl_setopt($ch, CURLOPT_READFUNCTION, function ($ch, $fd, $length) use ($body) {
return $body;
});
$response = array();
$response['body'] = '';
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) use (&$response) {
$response['body'] .= $chunk;
return strlen($chunk);
});
curl_exec($ch);
curl_close($ch);
$ch = curl_init("http://$AWS_LAMBDA_RUNTIME_API/2018-06-01/runtime/invocation/$invocation_id/response");
echo $response['body'];
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response['body']);
curl_exec($ch);
curl_close($ch);
}
?>