Skip to content

Commit 0a5f3b9

Browse files
committed
fix for #755
1 parent d52ad99 commit 0a5f3b9

File tree

13 files changed

+71
-24
lines changed

13 files changed

+71
-24
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ These are all the configuration options and their default value between brackets
7878
- "database": Database the connecting is made to (no default)
7979
- "tables": Comma separated list of tables to publish (defaults to 'all')
8080
- "middlewares": List of middlewares to load (`cors`)
81-
- "controllers": List of controllers to load (`records,geojson,openapi`)
81+
- "controllers": List of controllers to load (`records,geojson,openapi,status`)
8282
- "openApiBase": OpenAPI info (`{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}`)
8383
- "cacheType": `TempFile`, `Redis`, `Memcache`, `Memcached` or `NoCache` (`TempFile`)
8484
- "cachePath": Path/address of the cache (defaults to system's temp directory)
@@ -1216,6 +1216,32 @@ The following JSON structure is used:
12161216

12171217
NB: Any non-error response will have status: 200 OK
12181218

1219+
## Status
1220+
1221+
To connect to your monitoring there are two endpoints, one is up:
1222+
1223+
GET /status/up
1224+
1225+
And this should return status 200 and as data:
1226+
1227+
{
1228+
"db": true,
1229+
"cache": true
1230+
}
1231+
1232+
Values will be false when reading takes longer than 1 second. Alternatively you can use:
1233+
1234+
GET /status/ping
1235+
1236+
And this should return status 200 and as data:
1237+
1238+
{
1239+
"db": 42,
1240+
"cache": 9
1241+
}
1242+
1243+
These can be used to measure the time (in microseconds) to connect and read data from the database and the cache.
1244+
12191245
## Tests
12201246

12211247
I am testing mainly on Ubuntu and I have the following test setups:

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Tqdev\PhpCrudApi\Controller\JsonResponder;
1515
use Tqdev\PhpCrudApi\Controller\OpenApiController;
1616
use Tqdev\PhpCrudApi\Controller\RecordController;
17+
use Tqdev\PhpCrudApi\Controller\StatusController;
1718
use Tqdev\PhpCrudApi\Database\GenericDB;
1819
use Tqdev\PhpCrudApi\GeoJson\GeoJsonService;
1920
use Tqdev\PhpCrudApi\Middleware\AuthorizationMiddleware;
@@ -138,6 +139,9 @@ public function __construct(Config $config)
138139
$geoJson = new GeoJsonService($reflection, $records);
139140
new GeoJsonController($router, $responder, $geoJson);
140141
break;
142+
case 'status':
143+
new StatusController($router, $responder, $cache, $db);
144+
break;
141145
}
142146
}
143147
foreach ($config->getCustomControllers() as $className) {

src/Tqdev/PhpCrudApi/Cache/Cache.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ interface Cache
77
public function set(string $key, string $value, int $ttl = 0): bool;
88
public function get(string $key): string;
99
public function clear(): bool;
10+
public function ping(): int;
1011
}

src/Tqdev/PhpCrudApi/Cache/MemcacheCache.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Tqdev\PhpCrudApi\Cache;
44

5-
class MemcacheCache implements Cache
5+
use Tqdev\PhpCrudApi\Cache\Base\BaseCache;
6+
7+
class MemcacheCache extends BaseCache implements Cache
68
{
79
protected $prefix;
810
protected $memcache;

src/Tqdev/PhpCrudApi/Cache/NoCache.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,8 @@
22

33
namespace Tqdev\PhpCrudApi\Cache;
44

5-
class NoCache implements Cache
6-
{
7-
public function __construct()
8-
{
9-
}
10-
11-
public function set(string $key, string $value, int $ttl = 0): bool
12-
{
13-
return true;
14-
}
5+
use Tqdev\PhpCrudApi\Cache\Base\BaseCache;
156

16-
public function get(string $key): string
17-
{
18-
return '';
19-
}
20-
21-
public function clear(): bool
22-
{
23-
return true;
24-
}
7+
class NoCache extends BaseCache implements Cache
8+
{
259
}

src/Tqdev/PhpCrudApi/Cache/RedisCache.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tqdev\PhpCrudApi\Cache;
44

5+
use Tqdev\PhpCrudApi\Cache\Base\BaseCache;
6+
57
class RedisCache implements Cache
68
{
79
protected $prefix;

src/Tqdev/PhpCrudApi/Cache/TempFileCache.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Tqdev\PhpCrudApi\Cache;
44

5-
class TempFileCache implements Cache
5+
use Tqdev\PhpCrudApi\Cache\Base\BaseCache;
6+
7+
class TempFileCache extends BaseCache implements Cache
68
{
79
const SUFFIX = 'cache';
810

src/Tqdev/PhpCrudApi/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Config
1313
'database' => null,
1414
'tables' => '',
1515
'middlewares' => 'cors,errors',
16-
'controllers' => 'records,geojson,openapi',
16+
'controllers' => 'records,geojson,openapi,status',
1717
'customControllers' => '',
1818
'customOpenApiBuilders' => '',
1919
'cacheType' => 'TempFile',

src/Tqdev/PhpCrudApi/Controller/Responder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ interface Responder
99
public function error(int $error, string $argument, $details = null): ResponseInterface;
1010

1111
public function success($result): ResponseInterface;
12+
13+
public function multi($results): ResponseInterface;
14+
15+
public function exception($exception): ResponseInterface;
16+
1217
}

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ private function query(string $sql, array $parameters): \PDOStatement
341341
return $stmt;
342342
}
343343

344+
public function ping(): int
345+
{
346+
$start = microtime(true);
347+
$stmt = $this->pdo->prepare('SELECT 1');
348+
$stmt->execute();
349+
return intval((microtime(true)-$start)*1000000);
350+
}
351+
344352
public function getCacheKey(): string
345353
{
346354
return md5(json_encode([

src/Tqdev/PhpCrudApi/Record/RecordService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,9 @@ public function _list(string $tableName, array $params): ListDocument
135135
$this->joiner->addJoins($table, $records, $params, $this->db);
136136
return new ListDocument($records, $count);
137137
}
138+
139+
public function ping(): int
140+
{
141+
return $this->db->ping();
142+
}
138143
}

tests/config/base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'database' => 'incorrect_database',
44
'username' => 'incorrect_username',
55
'password' => 'incorrect_password',
6-
'controllers' => 'records,columns,cache,openapi,geojson',
6+
'controllers' => 'records,columns,cache,openapi,geojson,status',
77
'middlewares' => 'sslRedirect,xml,cors,reconnect,dbAuth,jwtAuth,basicAuth,authorization,sanitation,validation,ipAddress,multiTenancy,pageLimits,joinLimits,customization',
88
'dbAuth.mode' => 'optional',
99
'dbAuth.returnedColumns' => 'id,username,password',
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
===
2+
GET /status/up
3+
===
4+
200
5+
Content-Type: application/json; charset=utf-8
6+
Content-Length: 24
7+
8+
{"db":true,"cache":true}

0 commit comments

Comments
 (0)