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

28
app/helpers/logger.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
// Simple file logger
function log_start() {
$id = bin2hex(random_bytes(4));
$GLOBALS['req_start'] = microtime(true);
header("X-Request-Id: $id");
return $id;
}
function log_end($id, $status=200) {
$ms = round((microtime(true)-$GLOBALS['req_start'])*1000);
$ip = $_SERVER['REMOTE_ADDR'] ?? '-';
$path = $_SERVER['REQUEST_URI'] ?? '-';
$plan = $_SERVER['HTTP_X_DEWEMOJI_PLAN'] ?? ($GLOBALS['bucket_plan'] ?? '-');
$bucket = $GLOBALS['bucket_id'] ?? '-';
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '-';
$ref = $_SERVER['HTTP_REFERER'] ?? '-';
$errorMsg = '';
if ($status >= 400) {
$errorMsg = $GLOBALS['json_error_msg'] ?? '';
}
$line = sprintf("[%s] %s %s %s plan=%s bucket=%s %d %dms ua=\"%s\" ref=\"%s\"%s\n",
gmdate('c'), $id, $ip, $path, $plan, $bucket, $status, $ms, $ua, $ref,
$errorMsg ? " error=\"".addslashes($errorMsg)."\"" : ''
);
$logFile = __DIR__.'/../../storage/logs/access.log';
@mkdir(dirname($logFile), 0775, true);
error_log($line, 3, $logFile);
}