4
4
5
5
use Illuminate \Routing \Controller ;
6
6
use Illuminate \Support \Facades \DB ;
7
+ use Illuminate \Support \Facades \Route ;
8
+ use Illuminate \Support \Facades \Request ;
9
+ use Illuminate \Support \Facades \Storage ;
7
10
8
11
class HealthController extends Controller
9
12
{
@@ -23,9 +26,40 @@ public function index()
23
26
$ data ['database ' ] = $ database ;
24
27
}
25
28
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
+
26
55
return view ('health::index ' , compact ('data ' ));
27
56
}
28
57
58
+ /**
59
+ * Check database connection
60
+ *
61
+ * @return bool
62
+ */
29
63
private static function checkDataBase ()
30
64
{
31
65
try {
@@ -35,4 +69,47 @@ private static function checkDataBase()
35
69
return false ;
36
70
}
37
71
}
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
+ }
38
115
}
0 commit comments