Skip to content

Commit d4f4174

Browse files
committed
Implement API health route
1 parent 9cc0e7d commit d4f4174

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed

app/Providers/AppServiceProvider.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
namespace App\Providers;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Spatie\Health\Checks\Checks\DatabaseCheck;
7+
use Spatie\Health\Checks\Checks\EnvironmentCheck;
8+
use Spatie\Health\Checks\Checks\RedisCheck;
9+
use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck;
10+
use Spatie\Health\Facades\Health;
611

712
class AppServiceProvider extends ServiceProvider
813
{
@@ -19,6 +24,16 @@ public function register(): void
1924
*/
2025
public function boot(): void
2126
{
22-
//
27+
$env = match (config('app.url')) {
28+
'http://laravel-app.local' => 'local',
29+
default => 'production'
30+
};
31+
32+
Health::checks([
33+
EnvironmentCheck::new()->expectEnvironment($env),
34+
UsedDiskSpaceCheck::new(),
35+
DatabaseCheck::new(),
36+
RedisCheck::new()
37+
]);
2338
}
2439
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"laravel/sanctum": "^4.0",
1111
"laravel/tinker": "^2.9",
1212
"leventcz/laravel-top": "^1.1",
13-
"livewire/livewire": "^3.0",
13+
"spatie/laravel-health": "^1.30",
1414
"zircote/swagger-php": "^4.10"
1515
},
1616
"require-dev": {

config/health.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* A result store is responsible for saving the results of the checks. The
6+
* `EloquentHealthResultStore` will save results in the database. You
7+
* can use multiple stores at the same time.
8+
*/
9+
'result_stores' => [
10+
Spatie\Health\ResultStores\EloquentHealthResultStore::class => [
11+
'connection' => env('HEALTH_DB_CONNECTION', env('DB_CONNECTION')),
12+
'model' => Spatie\Health\Models\HealthCheckResultHistoryItem::class,
13+
'keep_history_for_days' => 5,
14+
],
15+
16+
/*
17+
Spatie\Health\ResultStores\CacheHealthResultStore::class => [
18+
'store' => 'file',
19+
],
20+
21+
Spatie\Health\ResultStores\JsonFileHealthResultStore::class => [
22+
'disk' => 's3',
23+
'path' => 'health.json',
24+
],
25+
26+
Spatie\Health\ResultStores\InMemoryHealthResultStore::class,
27+
*/
28+
],
29+
30+
/*
31+
* You can get notified when specific events occur. Out of the box you can use 'mail' and 'slack'.
32+
* For Slack you need to install laravel/slack-notification-channel.
33+
*/
34+
'notifications' => [
35+
/*
36+
* Notifications will only get sent if this option is set to `true`.
37+
*/
38+
'enabled' => false,
39+
40+
'notifications' => [
41+
Spatie\Health\Notifications\CheckFailedNotification::class => ['mail'],
42+
],
43+
44+
/*
45+
* Here you can specify the notifiable to which the notifications should be sent. The default
46+
* notifiable will use the variables specified in this config file.
47+
*/
48+
'notifiable' => Spatie\Health\Notifications\Notifiable::class,
49+
50+
/*
51+
* When checks start failing, you could potentially end up getting
52+
* a notification every minute.
53+
*
54+
* With this setting, notifications are throttled. By default, you'll
55+
* only get one notification per hour.
56+
*/
57+
'throttle_notifications_for_minutes' => 60,
58+
'throttle_notifications_key' => 'health:latestNotificationSentAt:',
59+
60+
'mail' => [
61+
'to' => 'your@example.com',
62+
63+
'from' => [
64+
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
65+
'name' => env('MAIL_FROM_NAME', 'Example'),
66+
],
67+
],
68+
69+
'slack' => [
70+
'webhook_url' => env('HEALTH_SLACK_WEBHOOK_URL', ''),
71+
72+
/*
73+
* If this is set to null the default channel of the webhook will be used.
74+
*/
75+
'channel' => null,
76+
77+
'username' => null,
78+
79+
'icon' => null,
80+
],
81+
],
82+
83+
/*
84+
* You can let Oh Dear monitor the results of all health checks. This way, you'll
85+
* get notified of any problems even if your application goes totally down. Via
86+
* Oh Dear, you can also have access to more advanced notification options.
87+
*/
88+
'oh_dear_endpoint' => [
89+
'enabled' => false,
90+
91+
/*
92+
* When this option is enabled, the checks will run before sending a response.
93+
* Otherwise, we'll send the results from the last time the checks have run.
94+
*/
95+
'always_send_fresh_results' => true,
96+
97+
/*
98+
* The secret that is displayed at the Application Health settings at Oh Dear.
99+
*/
100+
'secret' => env('OH_DEAR_HEALTH_CHECK_SECRET'),
101+
102+
/*
103+
* The URL that should be configured in the Application health settings at Oh Dear.
104+
*/
105+
'url' => '/oh-dear-health-check-results',
106+
],
107+
108+
/*
109+
* You can set a theme for the local results page
110+
*
111+
* - light: light mode
112+
* - dark: dark mode
113+
*/
114+
'theme' => 'light',
115+
116+
/*
117+
* When enabled, completed `HealthQueueJob`s will be displayed
118+
* in Horizon's silenced jobs screen.
119+
*/
120+
'silence_health_queue_job' => true,
121+
122+
/*
123+
* The response code to use for HealthCheckJsonResultsController when a health
124+
* check has failed
125+
*/
126+
'json_results_failure_status' => 200,
127+
];
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
use Spatie\Health\Models\HealthCheckResultHistoryItem;
7+
use Spatie\Health\ResultStores\EloquentHealthResultStore;
8+
9+
return new class extends Migration
10+
{
11+
public function up()
12+
{
13+
$connection = (new HealthCheckResultHistoryItem())->getConnectionName();
14+
$tableName = EloquentHealthResultStore::getHistoryItemInstance()->getTable();
15+
16+
Schema::connection($connection)->create($tableName, function (Blueprint $table) {
17+
$table->id();
18+
19+
$table->string('check_name');
20+
$table->string('check_label');
21+
$table->string('status');
22+
$table->text('notification_message')->nullable();
23+
$table->string('short_summary')->nullable();
24+
$table->json('meta');
25+
$table->timestamp('ended_at');
26+
$table->uuid('batch');
27+
28+
$table->timestamps();
29+
});
30+
31+
Schema::connection($connection)->table($tableName, function(Blueprint $table) {
32+
$table->index('created_at');
33+
$table->index('batch');
34+
});
35+
}
36+
};

routes/api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use App\Http\Controllers\RawgGamesController;
99
use App\Http\Controllers\RawgDomainController;
1010
use Illuminate\Support\Facades\Route;
11+
use Spatie\Health\Http\Controllers\HealthCheckJsonResultsController;
1112

1213
Route::permanentRedirect('/docs', '/swagger/index.html');
1314

15+
Route::get('/health', HealthCheckJsonResultsController::class);
1416
Route::post('/account/register', [AccountController::class, 'register']);
1517
Route::post('/auth/login', [AuthController::class, 'login']);
1618

0 commit comments

Comments
 (0)