Skip to content

Commit b2ca5fa

Browse files
committed
add route to fetch the current pod count, locked behind a key that lives in the config.php.
1 parent ee67b70 commit b2ca5fa

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

config.php.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343

4444
const PUBSUB_SERVER = "wss://pubsub:8080";
4545

46+
// Token / key that needs to be in the request to fetch the pod
47+
// count from the storage api
48+
const POD_COUNT_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
49+
4650
// Array of IPs that can be trusted. If they are in this list, they
4751
// won't be locked out after failed login attempts;
4852
const TRUSTED_IPS = [];

lib/Routes/SolidStorageProvider.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,31 @@ public static function respondToStorageNew() {
3131
header("Content-type: application/json");
3232
echo json_encode($responseData, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
3333
}
34+
35+
public static function respondToPodCount() {
36+
$requestFactory = new ServerRequestFactory();
37+
$rawRequest = $requestFactory->fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
38+
39+
if (!defined('POD_COUNT_KEY')) {
40+
header("HTTP/1.1 404 Not found");
41+
exit();
42+
}
43+
if (!isset($_SERVER['HTTP_POD_COUNT_KEY'])) {
44+
header("HTTP/1.1 404 Not found");
45+
exit();
46+
}
47+
if ($_SERVER['HTTP_POD_COUNT_KEY'] !== POD_COUNT_KEY) {
48+
header("HTTP/1.1 400 Bad Request");
49+
exit();
50+
}
51+
52+
$podCount = StorageServer::getPodCount();
53+
$responseData = array(
54+
"count" => $podCount
55+
);
56+
header("HTTP/1.1 200 OK");
57+
header("Content-type: application/json");
58+
echo json_encode($responseData, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
59+
}
3460
}
3561

lib/StorageServer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ public static function createStorage($ownerWebId) {
4848
];
4949
}
5050

51+
public static function getPodCount() {
52+
Db::connect();
53+
$query = Db::$pdo->prepare(
54+
'SELECT count(storage_id) as podCount FROM storage'
55+
);
56+
$query->execute();
57+
$result = $query->fetchAll();
58+
59+
if (sizeof($result) === 1) {
60+
return $result[0]['podCount'];
61+
}
62+
return false;
63+
}
64+
5165
public static function storageIdExists($storageId) {
5266
Db::connect();
5367
$query = Db::$pdo->prepare(

www/storage/index.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
switch($method) {
1818
case "GET":
1919
switch ($request) {
20+
case "/api/podcount":
21+
case "/api/podcount/":
22+
SolidStorageProvider::respondToPodCount();
23+
break;
2024
default:
2125
header($_SERVER['SERVER_PROTOCOL'] . " 404 Not found");
2226
break;

0 commit comments

Comments
 (0)