polish the api route, response, health, cache, and metrics

This commit is contained in:
dwindown
2025-08-31 20:45:24 +07:00
parent ce001bf9ce
commit 726f5a842f
12 changed files with 627 additions and 97 deletions

View File

View File

@@ -0,0 +1,43 @@
<?php
// public/index.php — tiny router
require __DIR__.'/../app/bootstrap.php';
require __DIR__.'/../app/helpers/cors.php';
require __DIR__.'/../app/helpers/logger.php';
require __DIR__.'/../app/helpers/json.php';
$reqId = log_start();
// Log at shutdown with final status code
register_shutdown_function(function() use ($reqId) {
$status = http_response_code();
if (function_exists('log_end')) log_end($reqId, $status ?: 200);
});
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?: '/';
// CORS preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { cors_preflight(); exit; }
// Routes (v1)
if ($path === '/v1/emojis') { require __DIR__.'/../app/controllers/Emojis.php'; exit; }
if (preg_match('#^/v1/emoji/([^/]+)$#', $path, $m)) {
$_GET['slug'] = urldecode($m[1] ?? '');
require __DIR__.'/../app/controllers/Emojis.php';
exit;
}
if ($path === '/v1/license/activate') { require __DIR__.'/../app/controllers/License.php'; exit; }
if ($path === '/v1/license/verify') { require __DIR__.'/../app/controllers/License.php'; exit; }
if ($path === '/v1/license/deactivate') { require __DIR__.'/../app/controllers/License.php'; exit; }
// Health
if ($path === '/v1/health') {
cors_allow();
header('Content-Type: application/json; charset=utf-8');
header('Vary: Origin,User-Agent');
echo json_encode(['ok'=>true,'time'=>gmdate('c')]);
exit;
}
if ($path === '/v1/metrics') { require __DIR__.'/../app/controllers/Metrics.php'; exit; }
if ($path === '/v1/metrics-lite') { require __DIR__.'/../app/controllers/MetricsLite.php'; exit; }
json_error(404, 'not_found', ['path' => $path]);