-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from VMGWARE/feature/API-Health-Check
Add 'Utilities' section to OpenAPI documentation configuration file to categorize API endpoints for fetching information
- Loading branch information
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\API; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\DB; | ||
use Vyuldashev\LaravelOpenApi\Attributes as OpenApi; | ||
use Illuminate\Http\JsonResponse; | ||
use PDO; | ||
|
||
#[OpenApi\PathItem] | ||
class HealthCheckController extends Controller | ||
{ | ||
/** | ||
* API Health Check | ||
* | ||
* @return JsonResponse | ||
*/ | ||
#[OpenApi\Operation(tags: ['Utilities'])] | ||
#[OpenApi\Response(factory: \App\OpenApi\Responses\Utilities\HealthCheck\ErrorResponse::class, statusCode: 503)] | ||
#[OpenApi\Response(factory: \App\OpenApi\Responses\Utilities\HealthCheck\SuccessResponse::class, statusCode: 200)] | ||
public function index(): JsonResponse | ||
{ | ||
// Status | ||
$status = 'ok'; | ||
|
||
// Dependencies | ||
$dependencies = [ | ||
'database' => DB::connection()->getPdo() ? 'OK' : 'Error', | ||
'storage' => is_writable(storage_path()) ? 'OK' : 'Error', | ||
]; | ||
|
||
// Check dependencies | ||
foreach ($dependencies as $dependency) { | ||
if ($dependency !== 'OK') { | ||
$status = 'error'; | ||
break; | ||
} | ||
} | ||
|
||
// Response | ||
$response = [ | ||
'status' => $status, | ||
'code' => $status === 'healthy' ? 200 : 503, | ||
'message' => $status === 'healthy' ? 'API v1 is up and running!' : 'API v1 is having issues.', | ||
'data' => [ | ||
'uptime' => 'N/A', // TODO: Get uptime from 'uptime' command | ||
'timestamp' => now()->toAtomString(), | ||
'app_version' => config('app.version'), | ||
'api_version' => 'v1', | ||
'diskspace' => disk_free_space('/') / disk_total_space('/') * 100, | ||
'latency' => round(microtime(true) - LARAVEL_START, 3), | ||
'dependencies' => $dependencies | ||
] | ||
]; | ||
|
||
return response()->json($response); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/app/OpenApi/Responses/Utilities/HealthCheck/ErrorResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\OpenApi\Responses\Utilities\HealthCheck; | ||
|
||
use GoldSpecDigital\ObjectOrientedOAS\Objects\MediaType; | ||
use GoldSpecDigital\ObjectOrientedOAS\Objects\Response; | ||
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema; | ||
use Vyuldashev\LaravelOpenApi\Factories\ResponseFactory; | ||
use Vyuldashev\LaravelOpenApi\Contracts\Reusable; | ||
|
||
class ErrorResponse extends ResponseFactory implements Reusable | ||
{ | ||
public function build(): Response | ||
{ | ||
$response = Schema::object()->properties( | ||
Schema::string('status')->example('error'), | ||
Schema::string('message')->example('API v1 is having issues.'), | ||
Schema::integer('code')->example(503), | ||
Schema::object('data')->properties( | ||
Schema::string('uptime')->example('3 days 2 hours'), | ||
Schema::string('timestamp')->example('2021-01-01 00:00:00'), | ||
Schema::string('app_version')->example('1.0.0'), | ||
Schema::string('api_version')->example('v1'), | ||
Schema::number('diskspace')->example(50.0), | ||
Schema::number('latency')->example(0.123) | ||
), | ||
Schema::object('dependencies')->properties( | ||
Schema::string('database')->example('Error'), | ||
Schema::string('storage')->example('OK'), | ||
) | ||
); | ||
|
||
return Response::create('GetHealthCheckError') | ||
->description('Get API Health Check status.') | ||
->content( | ||
MediaType::json()->schema($response) | ||
); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/app/OpenApi/Responses/Utilities/HealthCheck/SuccessResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\OpenApi\Responses\Utilities\HealthCheck; | ||
|
||
use GoldSpecDigital\ObjectOrientedOAS\Objects\MediaType; | ||
use GoldSpecDigital\ObjectOrientedOAS\Objects\Response; | ||
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema; | ||
use Vyuldashev\LaravelOpenApi\Factories\ResponseFactory; | ||
use Vyuldashev\LaravelOpenApi\Contracts\Reusable; | ||
|
||
class SuccessResponse extends ResponseFactory implements Reusable | ||
{ | ||
public function build(): Response | ||
{ | ||
$response = Schema::object()->properties( | ||
Schema::string('status')->example('ok'), | ||
Schema::string('message')->example('API v1 is up and running!'), | ||
Schema::integer('code')->example(200), | ||
Schema::object('data')->properties( | ||
Schema::string('uptime')->example('3 days 2 hours'), | ||
Schema::string('timestamp')->example('2021-01-01 00:00:00'), | ||
Schema::string('app_version')->example('1.0.0'), | ||
Schema::string('api_version')->example('v1'), | ||
Schema::number('diskspace')->example(50.0), | ||
Schema::number('latency')->example(0.123) | ||
), | ||
Schema::object('dependencies')->properties( | ||
Schema::string('database')->example('OK'), | ||
Schema::string('storage')->example('OK'), | ||
) | ||
); | ||
|
||
return Response::create('GetHealthCheckSuccess') | ||
->description('Get API Health Check status.') | ||
->content( | ||
MediaType::json()->schema($response) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters