Skip to content

Commit 59e2c5e

Browse files
authored
Merge pull request #5607 from BookStackApp/system_info_endpoint
API: System info endpoint
2 parents d29b14e + 0e69ab1 commit 59e2c5e

File tree

11 files changed

+102
-19
lines changed

11 files changed

+102
-19
lines changed

app/Api/ApiDocsGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Api;
44

5+
use BookStack\App\AppVersion;
56
use BookStack\Http\ApiController;
67
use Exception;
78
use Illuminate\Contracts\Container\BindingResolutionException;
@@ -25,7 +26,7 @@ class ApiDocsGenerator
2526
*/
2627
public static function generateConsideringCache(): Collection
2728
{
28-
$appVersion = trim(file_get_contents(base_path('version')));
29+
$appVersion = AppVersion::get();
2930
$cacheKey = 'api-docs::' . $appVersion;
3031
$isProduction = config('app.env') === 'production';
3132
$cacheVal = $isProduction ? Cache::get($cacheKey) : null;

app/App/AppVersion.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace BookStack\App;
4+
5+
class AppVersion
6+
{
7+
protected static string $version = '';
8+
9+
/**
10+
* Get the application's version number from its top-level `version` text file.
11+
*/
12+
public static function get(): string
13+
{
14+
if (!empty(static::$version)) {
15+
return static::$version;
16+
}
17+
18+
$versionFile = base_path('version');
19+
$version = trim(file_get_contents($versionFile));
20+
static::$version = $version;
21+
22+
return $version;
23+
}
24+
}

app/App/SystemApiController.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace BookStack\App;
4+
5+
use BookStack\Http\ApiController;
6+
use Illuminate\Http\JsonResponse;
7+
8+
class SystemApiController extends ApiController
9+
{
10+
/**
11+
* Read details regarding the BookStack instance.
12+
* Some details may be null where not set, like the app logo for example.
13+
*/
14+
public function read(): JsonResponse
15+
{
16+
$logoSetting = setting('app-logo', '');
17+
if ($logoSetting === 'none') {
18+
$logo = null;
19+
} else {
20+
$logo = $logoSetting ? url($logoSetting) : url('/logo.png');
21+
}
22+
23+
return response()->json([
24+
'version' => AppVersion::get(),
25+
'instance_id' => setting('instance-id'),
26+
'app_name' => setting('app-name'),
27+
'app_logo' => $logo,
28+
'base_url' => url('/'),
29+
]);
30+
}
31+
}

app/App/helpers.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use BookStack\App\AppVersion;
34
use BookStack\App\Model;
45
use BookStack\Facades\Theme;
56
use BookStack\Permissions\PermissionApplicator;
@@ -13,12 +14,7 @@
1314
*/
1415
function versioned_asset(string $file = ''): string
1516
{
16-
static $version = null;
17-
18-
if (is_null($version)) {
19-
$versionFile = base_path('version');
20-
$version = trim(file_get_contents($versionFile));
21-
}
17+
$version = AppVersion::get();
2218

2319
$additional = '';
2420
if (config('app.env') === 'development') {

app/Exceptions/BookStackExceptionHandlerPage.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Exceptions;
44

5+
use BookStack\App\AppVersion;
56
use Illuminate\Contracts\Foundation\ExceptionRenderer;
67

78
class BookStackExceptionHandlerPage implements ExceptionRenderer
@@ -30,9 +31,7 @@ protected function getEnvironment(): array
3031
return [
3132
'PHP Version' => phpversion(),
3233
'BookStack Version' => $this->safeReturn(function () {
33-
$versionFile = base_path('version');
34-
35-
return trim(file_get_contents($versionFile));
34+
return AppVersion::get();
3635
}, 'unknown'),
3736
'Theme Configured' => $this->safeReturn(function () {
3837
return config('view.theme');

app/Exports/ZipExports/ZipExportBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Exports\ZipExports;
44

5+
use BookStack\App\AppVersion;
56
use BookStack\Entities\Models\Book;
67
use BookStack\Entities\Models\Chapter;
78
use BookStack\Entities\Models\Page;
@@ -70,7 +71,7 @@ protected function build(): string
7071
$this->data['exported_at'] = date(DATE_ATOM);
7172
$this->data['instance'] = [
7273
'id' => setting('instance-id', ''),
73-
'version' => trim(file_get_contents(base_path('version'))),
74+
'version' => AppVersion::get(),
7475
];
7576

7677
$zipFile = tempnam(sys_get_temp_dir(), 'bszip-');

app/Settings/MaintenanceController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BookStack\Settings;
44

55
use BookStack\Activity\ActivityType;
6+
use BookStack\App\AppVersion;
67
use BookStack\Entities\Tools\TrashCan;
78
use BookStack\Http\Controller;
89
use BookStack\References\ReferenceStore;
@@ -19,14 +20,11 @@ public function index(TrashCan $trashCan)
1920
$this->checkPermission('settings-manage');
2021
$this->setPageTitle(trans('settings.maint'));
2122

22-
// Get application version
23-
$version = trim(file_get_contents(base_path('version')));
24-
2523
// Recycle bin details
2624
$recycleStats = $trashCan->getTrashedCounts();
2725

2826
return view('settings.maintenance', [
29-
'version' => $version,
27+
'version' => AppVersion::get(),
3028
'recycleStats' => $recycleStats,
3129
]);
3230
}

app/Settings/SettingController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BookStack\Settings;
44

55
use BookStack\Activity\ActivityType;
6+
use BookStack\App\AppVersion;
67
use BookStack\Http\Controller;
78
use BookStack\Users\Models\User;
89
use Illuminate\Http\Request;
@@ -26,12 +27,9 @@ public function category(string $category)
2627
$this->checkPermission('settings-manage');
2728
$this->setPageTitle(trans('settings.settings'));
2829

29-
// Get application version
30-
$version = trim(file_get_contents(base_path('version')));
31-
3230
return view('settings.categories.' . $category, [
3331
'category' => $category,
34-
'version' => $version,
32+
'version' => AppVersion::get(),
3533
'guestUser' => User::getGuest(),
3634
]);
3735
}

dev/api/responses/system-read.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": "v25.02.4",
3+
"instance_id": "1234abcd-cc12-7808-af0a-264cb0cbd611",
4+
"app_name": "My BookStack Instance",
5+
"app_logo": "https://docs.example.com/uploads/images/system/2025-05/cat-icon.png",
6+
"base_url": "https://docs.example.com"
7+
}

routes/api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use BookStack\Activity\Controllers\AuditLogApiController;
1010
use BookStack\Api\ApiDocsController;
11+
use BookStack\App\SystemApiController;
1112
use BookStack\Entities\Controllers as EntityControllers;
1213
use BookStack\Exports\Controllers as ExportControllers;
1314
use BookStack\Permissions\ContentPermissionApiController;
@@ -92,3 +93,5 @@
9293
Route::put('content-permissions/{contentType}/{contentId}', [ContentPermissionApiController::class, 'update']);
9394

9495
Route::get('audit-log', [AuditLogApiController::class, 'list']);
96+
97+
Route::get('system', [SystemApiController::class, 'read']);

0 commit comments

Comments
 (0)