The purpose of this tutorial to create a standalone health monitor for sites and applications on multiple domains.
On the route url of the site will be a rendered list for developers and colleagues.
This tutorial also adds a /ping endpoint that is used to receive “pong” in response.
As well as /json endpoint to receive the status data in JSON.
Create Laravel App & Install Packages
To get started we’ll need to set up a Laravel application, install Guzzle and ukfast/laravel-health-check:
1234567891011
laravel new health
cd health
cp .env.example .env
php artisan key:generate
composer require ukfast/laravel-health-check
composer require guzzlehttp/guzzle
php artisan vendor:publish --provider="UKFast\HealthCheck\HealthCheckServiceProvider" --tag="config"
Create Your First Health Check
We’ll create an example health check for this site.
You should change J84115 to a namespace for the app or business as well as the class name, $name, $domain endpoint.
1234567891011121314151617181920212223
<?phpnamespaceApp\HealthChecks\J84115;useIlluminate\Support\Facades\Http;useUKFast\HealthCheck\HealthCheck;classJamesBallSiteHealthCheckextendsHealthCheck{public$name='jb-site';public$domain="https://james-ball.co.uk/ping";publicfunctionstatus(){$response=Http::get($this->domain);if($response->successful()){return$this->okay();}else{return$this->problem("Failed to connect to $this->domain");}}}
Register Your check
Open config/healthcheck.php and add the HealthCheck class namespace entry to the checks index or replace the existing data:
1234567
/* * List of health checks to run when determining the health * of the service */'checks'=>[App\HealthChecks\J84115\JamesBallSiteHealthCheck::class,],
And if required you can: update the route-paths.health value to json, this will allow you to access the data as JSON at {app_url}/json.
1234567
/** * Paths to host the health check and ping endpoints */'route-paths'=>['health'=>'/json','ping'=>'/ping',],
Create a Page to render the check(s)
Create the following file resources/views/health.blade.php: