Skip to content

Commit 4811613

Browse files
committed
[#.x] - health and 404 middleware
1 parent 2c80c47 commit 4811613

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Domain\Middleware;
15+
16+
use Phalcon\Api\Domain\Services\Container;
17+
use Phalcon\Api\Domain\Services\Http\Response;
18+
use Phalcon\Mvc\Micro;
19+
use Phalcon\Mvc\Micro\MiddlewareInterface;
20+
21+
abstract class AbstractMiddleware implements MiddlewareInterface
22+
{
23+
protected function halt(
24+
Micro $application,
25+
int $code,
26+
string $message = '',
27+
array $data = [],
28+
array $errors = []
29+
): void {
30+
/** @var Response $response */
31+
$response = $application->getSharedService(Container::RESPONSE);
32+
33+
$application->stop();
34+
35+
$response->withCode($code, $message);
36+
37+
if (true === empty($errors)) {
38+
$response->withPayloadData($data);
39+
} else {
40+
$response->withPayloadErrors($errors);
41+
}
42+
43+
$response->render()->send();
44+
}
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Domain\Middleware;
15+
16+
use Phalcon\Api\Domain\Services\Container;
17+
use Phalcon\Api\Domain\Services\Http\HttpCodesEnum;
18+
use Phalcon\Events\Exception as EventsException;
19+
use Phalcon\Http\Request;
20+
use Phalcon\Http\Response\Exception;
21+
use Phalcon\Mvc\Micro;
22+
23+
final class HealthMiddleware extends AbstractMiddleware
24+
{
25+
/**
26+
* @param Micro $application
27+
*
28+
* @return true
29+
* @throws EventsException
30+
* @throws Exception
31+
*/
32+
public function call(Micro $application): bool
33+
{
34+
/** @var Request $request */
35+
$request = $application->getSharedService(Container::REQUEST);
36+
37+
if (
38+
'/health' === $request->getURI() &&
39+
true === $request->isGet()
40+
) {
41+
$payload = [
42+
'status' => 'ok',
43+
'message' => 'service operational',
44+
];
45+
46+
$this->halt(
47+
$application,
48+
HttpCodesEnum::OK->value,
49+
HttpCodesEnum::OK->text(),
50+
$payload
51+
);
52+
}
53+
54+
return true;
55+
}
56+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Domain\Middleware;
15+
16+
use Phalcon\Api\Domain\Services\Http\HttpCodesEnum;
17+
use Phalcon\Events\Event;
18+
use Phalcon\Events\Exception as EventsException;
19+
use Phalcon\Http\Response\Exception;
20+
use Phalcon\Mvc\Micro;
21+
22+
final class NotFoundMiddleware extends AbstractMiddleware
23+
{
24+
public function beforeNotFound(Event $event, Micro $application): bool
25+
{
26+
$this->halt(
27+
$application,
28+
HttpCodesEnum::NotFound->value,
29+
'error',
30+
[],
31+
HttpCodesEnum::AppResourceNotFound->error()
32+
);
33+
34+
return false;
35+
}
36+
37+
/**
38+
* @param Micro $application
39+
*
40+
* @return true
41+
* @throws EventsException
42+
* @throws Exception
43+
*/
44+
public function call(Micro $application): bool
45+
{
46+
return true;
47+
}
48+
}

0 commit comments

Comments
 (0)