generated from mnapoli/project-template
-
-
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.
- Loading branch information
Showing
15 changed files
with
310 additions
and
67 deletions.
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,36 @@ | ||
{ | ||
"name": "mnapoli/myproject", | ||
"description": "Give it a nice description!", | ||
"name": "bref/laravel-health-check", | ||
"description": "Bref health checks for Laravel applications", | ||
"keywords": [], | ||
"license": "MIT", | ||
"type": "library", | ||
"autoload": { | ||
"psr-4": { | ||
"MyProject\\": "src/" | ||
"Bref\\LaravelHealthCheck\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"MyProject\\Test\\": "tests/" | ||
"Bref\\LaravelHealthCheck\\Test\\": "tests/" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=8.0" | ||
"php": ">=8.0", | ||
"illuminate/console": "^8.0 || ^9.0 || ^10.0 || ^11.0", | ||
"illuminate/contracts": "^8.0 || ^9.0 || ^10.0 || ^11.0", | ||
"illuminate/database": "^8.0 || ^9.0 || ^10.0 || ^11.0", | ||
"illuminate/http": "^8.0 || ^9.0 || ^10.0 || ^11.0", | ||
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.0", | ||
"mnapoli/hard-mode": "^0.3.0", | ||
"phpstan/phpstan": "^1" | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"dealerdirect/phpcodesniffer-composer-installer": true | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Bref\\LaravelHealthCheck\\BrefHealthCheckServiceProvider" | ||
] | ||
} | ||
} | ||
} |
Empty file.
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,16 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck; | ||
|
||
use Bref\LaravelHealthCheck\Commands\BrefHealthCheck; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class BrefHealthCheckServiceProvider extends ServiceProvider | ||
{ | ||
public function register(): void | ||
{ | ||
$this->commands([ | ||
BrefHealthCheck::class, | ||
]); | ||
} | ||
} |
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,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck; | ||
|
||
abstract class Check | ||
{ | ||
abstract public function getName(): string; | ||
|
||
abstract public function check(): CheckResult; | ||
|
||
public function ok(?string $message = null): CheckResult | ||
{ | ||
return CheckResult::ok($this->getName(), $message); | ||
} | ||
|
||
public function warning(?string $message = null): CheckResult | ||
{ | ||
return CheckResult::error($this->getName(), $message); | ||
} | ||
|
||
public function error(?string $message = null): CheckResult | ||
{ | ||
return CheckResult::error($this->getName(), $message); | ||
} | ||
} |
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,51 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck; | ||
|
||
class CheckResult | ||
{ | ||
private const STATUS_OK = 'ok'; | ||
private const STATUS_WARNING = 'warning'; | ||
private const STATUS_ERROR = 'error'; | ||
|
||
/** @readonly */ | ||
public string $name; | ||
/** @readonly */ | ||
public string $status; | ||
/** @readonly */ | ||
public ?string $message; | ||
|
||
public function __construct(string $name, string $status, ?string $message = null) | ||
{ | ||
$this->name = $name; | ||
$this->status = $status; | ||
$this->message = $message; | ||
} | ||
|
||
public static function ok(string $name, ?string $message = null): self | ||
{ | ||
return new self($name, self::STATUS_OK, $message); | ||
} | ||
|
||
public static function warning(string $name, ?string $message = null): self | ||
{ | ||
return new self($name, self::STATUS_WARNING, $message); | ||
} | ||
|
||
public static function error(string $name, ?string $message = null): self | ||
{ | ||
return new self($name, self::STATUS_ERROR, $message); | ||
} | ||
|
||
/** | ||
* @return array{status: string, name: string, message: string|null} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'name' => $this->name, | ||
'status' => $this->status, | ||
'message' => $this->message, | ||
]; | ||
} | ||
} |
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,37 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck\Checks; | ||
|
||
use Bref\LaravelHealthCheck\Check; | ||
use Bref\LaravelHealthCheck\CheckResult; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Str; | ||
|
||
class CacheConnection extends Check | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'Cache connection'; | ||
} | ||
|
||
public function check(): CheckResult | ||
{ | ||
if (! $this->canWriteValuesToCache()) { | ||
return $this->error(); | ||
} | ||
|
||
return $this->ok(); | ||
} | ||
|
||
protected function canWriteValuesToCache(): bool | ||
{ | ||
$value = Str::random(5); | ||
$key = "bref:health-check-$value"; | ||
|
||
Cache::put($key, $value, 10); | ||
$actualValue = Cache::get($key); | ||
Cache::forget($key); | ||
|
||
return $actualValue === $value; | ||
} | ||
} |
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,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck\Checks; | ||
|
||
use Bref\LaravelHealthCheck\Check; | ||
use Bref\LaravelHealthCheck\CheckResult; | ||
use Illuminate\Support\Facades\DB; | ||
use Throwable; | ||
|
||
class DatabaseConnection extends Check | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'Database connection'; | ||
} | ||
|
||
public function check(): CheckResult | ||
{ | ||
try { | ||
DB::connection()->getPdo(); | ||
|
||
return $this->ok(); | ||
} catch (Throwable $exception) { | ||
return $this->error("Could not connect to the database: `{$exception->getMessage()}`"); | ||
} | ||
} | ||
} |
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,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck\Checks; | ||
|
||
use Bref\LaravelHealthCheck\Check; | ||
use Bref\LaravelHealthCheck\CheckResult; | ||
|
||
class DebugModeIsDisabled extends Check | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'Debug mode is disabled'; | ||
} | ||
|
||
public function check(): CheckResult | ||
{ | ||
return config('app.debug') ? $this->error() : $this->ok(); | ||
} | ||
} |
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,30 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck\Checks; | ||
|
||
use Bref\LaravelHealthCheck\Check; | ||
use Bref\LaravelHealthCheck\CheckResult; | ||
use Illuminate\Support\Facades\Http; | ||
use Throwable; | ||
|
||
class InternetConnectivity extends Check | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'Internet connectivity'; | ||
} | ||
|
||
public function check(): CheckResult | ||
{ | ||
try { | ||
$status = Http::timeout(3) | ||
->connectTimeout(3) | ||
->get('https://google.com') | ||
->successful(); | ||
} catch (Throwable $e) { | ||
return $this->error($e->getMessage()); | ||
} | ||
|
||
return $status ? $this->ok() : $this->error(); | ||
} | ||
} |
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,26 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck\Checks; | ||
|
||
use Bref\LaravelHealthCheck\Check; | ||
use Bref\LaravelHealthCheck\CheckResult; | ||
|
||
class LambdaMemoryLimit extends Check | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'Memory available is sufficient'; | ||
} | ||
|
||
public function check(): CheckResult | ||
{ | ||
$memoryAvailableInMb = (int) ($_SERVER['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] ?? 0); | ||
$recommendedMemory = 1024; | ||
|
||
if ($memoryAvailableInMb < $recommendedMemory) { | ||
return $this->error("The memory limit configured is $memoryAvailableInMb MB, but $recommendedMemory MB is recommended."); | ||
} | ||
|
||
return $this->ok(); | ||
} | ||
} |
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,23 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bref\LaravelHealthCheck\Checks; | ||
|
||
use Bref\LaravelHealthCheck\Check; | ||
use Bref\LaravelHealthCheck\CheckResult; | ||
|
||
class PhpVersionIsRecentEnough extends Check | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'PHP version is recent enough'; | ||
} | ||
|
||
public function check(): CheckResult | ||
{ | ||
if (PHP_VERSION_ID < 80100) { | ||
return $this->error('PHP version is no longer supported, upgrade to PHP 8.1 or newer'); | ||
} | ||
|
||
return $this->ok(); | ||
} | ||
} |
Oops, something went wrong.