22
33namespace BNETDocs \Controllers ;
44
5+ use \BNETDocs \Libraries \Cache ;
56use \BNETDocs \Libraries \Common ;
67use \BNETDocs \Libraries \Controller ;
8+ use \BNETDocs \Libraries \Database ;
9+ use \BNETDocs \Libraries \DatabaseDriver ;
710use \BNETDocs \Libraries \Exceptions \UnspecifiedViewException ;
811use \BNETDocs \Libraries \Router ;
912use \BNETDocs \Models \Status as StatusModel ;
1013use \BNETDocs \Views \StatusJSON as StatusJSONView ;
1114use \BNETDocs \Views \StatusPlain as StatusPlainView ;
1215use \DateTime ;
1316use \DateTimeZone ;
17+ use \StdClass ;
1418
1519class Status extends Controller {
1620
@@ -26,21 +30,34 @@ public function run(Router &$router) {
2630 throw new UnspecifiedViewException ();
2731 }
2832 $ model = new StatusModel ();
29- $ this ->getStatus ($ model );
33+ $ code = (! $ this ->getStatus ($ model) ? 500 : 200 );
3034 ob_start ();
3135 $ view ->render ($ model );
32- $ router ->setResponseCode (200 );
36+ $ router ->setResponseCode ($ code );
3337 $ router ->setResponseTTL (300 );
3438 $ router ->setResponseHeader ("Content-Type " , $ view ->getMimeType ());
3539 $ router ->setResponseContent (ob_get_contents ());
3640 ob_end_clean ();
3741 }
3842
3943 protected function getStatus (StatusModel &$ model ) {
44+ if (!isset (Common::$ database )) {
45+ Common::$ database = DatabaseDriver::getDatabaseObject ();
46+ }
47+
48+ $ healthcheck = new StdClass ();
49+ $ healthcheck ->database = (Common::$ database instanceof Database);
50+ $ healthcheck ->memcache = (Common::$ cache instanceof Cache );
51+
52+ $ model ->healthcheck = $ healthcheck ;
4053 $ model ->remote_address = getenv ("REMOTE_ADDR " );
4154 $ model ->remote_geoinfo = geoip_record_by_name ($ model ->remote_address );
4255 $ model ->timestamp = new DateTime ("now " , new DateTimeZone ("UTC " ));
4356 $ model ->version_info = Common::$ version ;
57+
58+ foreach ($ healthcheck as $ key => $ val ) {
59+ if (is_bool ($ val ) && !$ val ) return false ;
60+ }
4461 return true ;
4562 }
4663
0 commit comments