Skip to content

Commit de0f44c

Browse files
committed
Implement health check endpoint
1 parent a84de86 commit de0f44c

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,35 @@ You no longer need to stack `jwt.auth` + `load.access` manually—just use `micr
251251

252252
---
253253

254+
## Endpoints
255+
256+
### Health Check Endpoint
257+
258+
This package exposes a JSON endpoint at `/api/health` providing basic service details.
259+
260+
#### Configuration (`config/microservice.php`)
261+
262+
```php
263+
'health' => [
264+
'enabled' => env('HEALTH_ENDPOINT_ENABLED', true),
265+
'path' => '/api/health',
266+
],
267+
```
268+
269+
When enabled (default), visiting `/api/health` returns:
270+
271+
```json
272+
{
273+
"status": "ok",
274+
"app": "your-app-name",
275+
"environment": "testing",
276+
"laravel": "12.x-dev",
277+
"timestamp": "2025-01-01T12:00:00Z"
278+
}
279+
```
280+
281+
---
282+
254283
## Public Release and Future Goals
255284

256285
This repository is brand new, and I’m excited to develop it further! My plan is to continuously strengthen the core, add more middleware modules, expand test coverage, and refine configuration options.

src/Http/HealthCheckController.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Kroderdev\LaravelMicroserviceCore\Http;
4+
5+
use Illuminate\Http\JsonResponse;
6+
7+
class HealthCheckController
8+
{
9+
public function __invoke(): JsonResponse
10+
{
11+
return response()->json([
12+
'status' => 'ok',
13+
'app' => config('app.name'),
14+
'environment' => app()->environment(),
15+
'laravel' => app()->version(),
16+
'timestamp' => now()->toIso8601String(),
17+
]);
18+
}
19+
}

src/Providers/MicroserviceServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Http;
88
use Illuminate\Support\ServiceProvider;
99
use Kroderdev\LaravelMicroserviceCore\Contracts\ApiGatewayClientInterface;
10+
use Kroderdev\LaravelMicroserviceCore\Http\HealthCheckController;
1011
use Kroderdev\LaravelMicroserviceCore\Http\Middleware\LoadAccess;
1112
use Kroderdev\LaravelMicroserviceCore\Http\Middleware\PermissionMiddleware;
1213
use Kroderdev\LaravelMicroserviceCore\Http\Middleware\RoleMiddleware;
@@ -78,6 +79,12 @@ public function boot(Router $router): void
7879
LoadAccess::class,
7980
]);
8081

82+
// Health check route
83+
if (config('microservice.health.enabled', true)) {
84+
$path = ltrim(config('microservice.health.path', '/api/health'), '/');
85+
$router->get('/' . $path, HealthCheckController::class);
86+
}
87+
8188
// HTTP
8289
Http::macro('apiGateway', function () {
8390
// Correlation ID

src/config/microservice.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@
117117
'url' => env('API_GATEWAY_URL', 'http://gateway.local'),
118118
],
119119

120+
/*
121+
|--------------------------------------------------------------------------
122+
| Health Endpoint
123+
|--------------------------------------------------------------------------
124+
|
125+
| Enable or disable registration of the default /api/health route.
126+
|
127+
*/
128+
'health' => [
129+
'enabled' => env('HEALTH_ENDPOINT_ENABLED', true),
130+
'path' => '/api/health',
131+
],
132+
120133
/*
121134
|--------------------------------------------------------------------------
122135
| Permissions Cache

tests/Http/HealthCheckTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Tests\Http;
4+
5+
use Orchestra\Testbench\TestCase;
6+
use Kroderdev\LaravelMicroserviceCore\Providers\MicroserviceServiceProvider;
7+
8+
class HealthCheckTest extends TestCase
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return [MicroserviceServiceProvider::class];
13+
}
14+
15+
/** @test */
16+
public function health_endpoint_returns_200()
17+
{
18+
$response = $this->get('/api/health');
19+
20+
$response->assertStatus(200)
21+
->assertJsonStructure([
22+
'status',
23+
'app',
24+
'environment',
25+
'laravel',
26+
'timestamp',
27+
]);
28+
}
29+
}

0 commit comments

Comments
 (0)