Skip to content

Commit 75a4af6

Browse files
Modify health controller:
Add check migrations, Add check routes.
1 parent 26290d4 commit 75a4af6

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/Http/Controllers/HealthController.php

+77
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Illuminate\Routing\Controller;
66
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Route;
8+
use Illuminate\Support\Facades\Request;
9+
use Illuminate\Support\Facades\Storage;
710

811
class HealthController extends Controller
912
{
@@ -23,9 +26,40 @@ public function index()
2326
$data['database'] = $database;
2427
}
2528

29+
if (config('health.migrations')) {
30+
$migrations = [
31+
'status' => true,
32+
'data' => null,
33+
];
34+
35+
if (! self::checkMigrataions()) {
36+
$migrations['status'] = false;
37+
$migrations['data'] = 'در اجرا مایگریشن‌ها مشکلی وجود دارد';
38+
}
39+
$data['migrations'] = $migrations;
40+
}
41+
42+
if (config('health.routes')) {
43+
$routes = [
44+
'status' => true,
45+
'data' => null,
46+
];
47+
48+
if (! self::checkRoutes()) {
49+
$routes['status'] = false;
50+
$routes['data'] = 'روت‌ها در درسترس نیستند';
51+
}
52+
$data['routes'] = $routes;
53+
}
54+
2655
return view('health::index', compact('data'));
2756
}
2857

58+
/**
59+
* Check database connection
60+
*
61+
* @return bool
62+
*/
2963
private static function checkDataBase()
3064
{
3165
try {
@@ -35,4 +69,47 @@ private static function checkDataBase()
3569
return false;
3670
}
3771
}
72+
73+
/**
74+
* Check migrations
75+
*
76+
* @return bool
77+
*/
78+
private static function checkMigrataions()
79+
{
80+
$migrations = DB::table('migrations')->get();
81+
$files = Storage::files(base_path() . '/database/migrations/');
82+
83+
if (count($migrations) === count($files))
84+
return true;
85+
86+
return false;
87+
}
88+
89+
/**
90+
* Check routes
91+
*
92+
* @return bool
93+
*/
94+
private static function checkRoutes()
95+
{
96+
$blackList = ['health'];
97+
$routes = Route::getRoutes()->getRoutesByMethod()['GET'];
98+
99+
foreach ($routes as $uri => $route) {
100+
if (in_array($route->uri(), $blackList) || $route->getPrefix() === 'api') {
101+
continue;
102+
}
103+
104+
$response = app()->handle(Request::create(url($route->uri())));
105+
$params = $route->parameters ?? [];
106+
107+
if (count($params)) continue;
108+
109+
$status = $response->status();
110+
if ($status != 200) return false;
111+
}
112+
113+
return true;
114+
}
38115
}

0 commit comments

Comments
 (0)