-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.php
172 lines (141 loc) · 4.89 KB
/
index.php
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
<?php declare(strict_types=1);
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use GuzzleHttp\Client;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Contracts\Cache\ItemInterface;
error_reporting(E_ALL ^ E_DEPRECATED);
require_once __DIR__ . '/vendor/autoload.php';
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => false,
],
]);
$container = $app->getContainer();
$container['view'] = function () {
return new \Slim\Views\Twig(__DIR__ . '/templates');
};
$regions = [
'ca-central-1',
'eu-central-1',
'eu-north-1',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'eu-south-1',
'eu-south-2',
'sa-east-1',
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2',
'ap-east-1',
'af-south-1',
'ap-south-1',
'ap-northeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ap-southeast-1',
'ap-southeast-2',
'me-south-1',
];
$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) use ($regions) {
$selectedRegion = $request->getQueryParams()['region'] ?? 'us-east-1';
if (!in_array($selectedRegion, $regions)) {
$response->getBody()->write('Unknown region');
return $response;
}
$versions = listVersions();
$selectedVersion = $request->getQueryParams()['version'] ?? $versions[array_key_first($versions)];
if (!in_array($selectedVersion, $versions)) {
$response->getBody()->write('Unknown version');
return $response;
}
return $this->view->render($response, 'index.html.twig', [
'layers' => listLayers($selectedVersion, $selectedRegion),
'versions' => $versions,
'regions' => $regions,
'selectedRegion' => $selectedRegion,
'selectedVersion' => $selectedVersion,
]);
});
$app->get('/embedded', function (ServerRequestInterface $request, ResponseInterface $response) use ($regions) {
$selectedRegion = $request->getQueryParams()['region'] ?? 'us-east-1';
if (!in_array($selectedRegion, $regions)) {
$response->getBody()->write('Unknown region');
return $response;
}
$versions = listVersions();
$latestVersion = $versions[array_key_first($versions) + 1];
return $this->view->render($response, 'embedded.html.twig', [
'layers' => listLayers($latestVersion, $selectedRegion),
'regions' => $regions,
'selectedRegion' => $selectedRegion,
]);
});
$app->run();
function listLayers(string $version, string $region): array
{
$cache = new FilesystemAdapter();
// Caching network calls to improve performance
$data = $cache->get('layers_' . $version, function (ItemInterface $item) use ($version) {
$item->expiresAfter(3600);
$client = new Client();
$url = 'https://raw.githubusercontent.com/brefphp/bref/' . $version . '/layers.json';
$response = $client->get($url);
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('Could not fetch layers from GitHub');
}
$json = $response->getBody()->getContents();
$data = json_decode($json, true);
return $data;
});
$layers = [];
$accountId = '209497400698';
if ($version === 'v2' || strpos($version, '2.') === 0) {
$accountId = '534081306603';
}
foreach ($data as $name => $regions) {
if (!isset($regions[$region])) {
continue;
}
$layers[] = [
'name' => $name,
'arn' => sprintf('arn:aws:lambda:%s:%s:layer:%s:%s', $region, $accountId, $name, $regions[$region]),
'version' => $regions[$region],
];
}
return $layers;
}
function listVersions(): array
{
$cache = new FilesystemAdapter();
// Caching network call as GitHub's API is rate limited
$releases = $cache->get('releases', function (ItemInterface $item) {
$item->expiresAfter(3600);
$client = new Client();
$url = 'https://api.github.com/repos/brefphp/bref/releases';
$response = $client->get($url);
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('Could not fetch releases from GitHub');
}
$json = $response->getBody()->getContents();
$releases = json_decode($json, true);
return $releases;
});
$versions = [];
foreach ($releases as $release) {
// Skip prereleases (e.g., 0.5.14-beta1)
if ($release['prerelease']) {
continue;
}
// Skip releases prior to 0.5.0 as that's when layers.json was added
if (\Composer\Semver\Comparator::lessThan($release['name'], '0.5.0')) {
continue;
}
$versions[] = $release['name'];
}
// Sorting is needed as minor/patch releases can be published after major/minor releases
$versions = \Composer\Semver\Semver::rsort($versions);
return $versions;
}