renoki-co / laravel-healthchecks
Laravel Healthchecks is a simple controller class that helps you build your own healthchecks endpoint without issues.
Fund package maintenance!
rennokki
Installs: 52 049
Dependents: 0
Suggesters: 0
Security: 0
Stars: 55
Watchers: 4
Forks: 5
Open Issues: 5
Requires
- illuminate/routing: ^8.83|^9.0.1
- illuminate/support: ^8.83|^9.0.1
Requires (Dev)
- laravel/legacy-factories: ^1.3
- mockery/mockery: ^1.5
- orchestra/testbench: ^6.28|^7.0
- orchestra/testbench-core: ^6.28|^7.0
- phpunit/phpunit: ^9.5.13
This package is auto-updated.
Last update: 2024-12-08 06:53:21 UTC
README
Laravel Healthchecks is a simple controller class that helps you build your own healthchecks endpoint without issues.
🤝 Supporting
If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦
🚀 Installation
You can install the package via composer:
composer require renoki-co/laravel-healthchecks
🙌 Usage
First of all, you should create your own Controller for healthchecks, that extends the RenokiCo\LaravelHealthchecks\Http\Controllers\HealthcheckController
.
use Illuminate\Http\Request; use RenokiCo\LaravelHealthchecks\Http\Controllers\HealthcheckController; class MyHealthcheckController extends HealthcheckController { /** * Register the healthchecks. * * @param \Illuminate\Http\Request $request * @return void */ public function registerHealthchecks(Request $request) { $this->addHealthcheck('mysql', function (Request $request) { // Try testing the MySQL connection here // and return true/false for pass/fail. return true; }); } }
// In your routes Route::get('/healthcheck', [MyHealthcheckController::class, 'handle']);
Registering healthchecks
Within the controller, you should register the healthchecks closures in the registerHealthchecks
method, like the example stated above.
You can add as many healthchecks as you want.
public function registerHealthchecks(Request $request) { $this->addHealthcheck('mysql', function (Request $request) { // }); $this->addHealthcheck('redis', function (Request $request) { // }); $this->addHealthcheck('some_check', function (Request $request) { // }); $this->addHealthcheck('another_check_here', function (Request $request) { // }); }
Status Codes
In case of failure, the response is 500
. For all successful responses, the status code is 200
.
To change the http codes being sent out, specify this in your registerHealthchecks
method:
public function registerHealthchecks(Request $request) { $this->setPassingHttpCode(203); $this->setFailingHttpCode(403); $this->addHealthcheck('mysql', function (Request $request) { return true; }); }
Outputs
By default, the output will be OK
or FAIL
as string, but in case you want to debug the healthchecks, you can get a JSON with each registered healthchecks and their pass/fail closures.
You have just to call withOutput()
:
public function registerHealthchecks(Request $request) { $this->withOutput(); $this->addHealthcheck('mysql', function (Request $request) { return true; }); $this->addHealthcheck('redis', function (Request $request) { return false; }); }
The output in the browser would be like this:
{ "mysql": true, "redis": false }
🐛 Testing
vendor/bin/phpunit
🤝 Contributing
Please see CONTRIBUTING for details.
🔒 Security
If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.