]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/StatusController.php
Added status endpoint
[bookstack] / app / Http / Controllers / StatusController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Illuminate\Support\Facades\Cache;
4 use Illuminate\Support\Facades\DB;
5 use Illuminate\Support\Facades\Session;
6 use Illuminate\Support\Str;
7
8 class StatusController extends Controller
9 {
10
11     /**
12      * Show the system status as a simple json page.
13      */
14     public function show()
15     {
16         $statuses = [
17             'database' => $this->trueWithoutError(function () {
18                 return DB::table('migrations')->count() > 0;
19             }),
20             'cache' => $this->trueWithoutError(function () {
21                 $rand = Str::random();
22                 Cache::set('status_test', $rand);
23                 return Cache::get('status_test') === $rand;
24             }),
25             'session' => $this->trueWithoutError(function () {
26                 $rand = Str::random();
27                 Session::put('status_test', $rand);
28                 return Session::get('status_test') === $rand;
29             }),
30         ];
31
32         $hasError = in_array(false, $statuses);
33         return response()->json($statuses, $hasError ? 500 : 200);
34     }
35
36     /**
37      * Check the callable passed returns true and does not throw an exception.
38      */
39     protected function trueWithoutError(callable $test): bool
40     {
41         try {
42             return $test() === true;
43         } catch (\Exception $e) {
44             return false;
45         }
46     }
47 }