11111
This commit is contained in:
parent
5514a47ffb
commit
cf106d1228
34
source.js
34
source.js
@ -21,11 +21,15 @@ const viewsInfo = {
|
|||||||
apiCall: 0,
|
apiCall: 0,
|
||||||
// 缓存调用次数
|
// 缓存调用次数
|
||||||
cacheCall: 0,
|
cacheCall: 0,
|
||||||
|
// 缓存读取错误次数
|
||||||
|
cacheReadError: 0,
|
||||||
|
// API 调用错误次数
|
||||||
|
fetchApiError: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// 默认端口号和 API 地址
|
// 默认端口号和 API 地址
|
||||||
let port = 9001;
|
let port = 9001;
|
||||||
let apiEndpoint = 'https://x-mo.cn:9001/get/';
|
let apiEndpoint = 'https://oss.x-php.com/get/';
|
||||||
|
|
||||||
// 解析命令行参数
|
// 解析命令行参数
|
||||||
args.forEach(arg => {
|
args.forEach(arg => {
|
||||||
@ -170,7 +174,6 @@ const server = http.createServer(async (req, res) => {
|
|||||||
|
|
||||||
// 修改 pathIndex 记录时,添加时间戳
|
// 修改 pathIndex 记录时,添加时间戳
|
||||||
pathIndex[uniqidhex] = { uniqid: data.uniqid, timestamp: Date.now() };
|
pathIndex[uniqidhex] = { uniqid: data.uniqid, timestamp: Date.now() };
|
||||||
|
|
||||||
cacheMetaFile = pathModule.join(cacheDir, `${data.uniqid}.meta`);
|
cacheMetaFile = pathModule.join(cacheDir, `${data.uniqid}.meta`);
|
||||||
cacheContentFile = pathModule.join(cacheDir, `${data.uniqid}.content`);
|
cacheContentFile = pathModule.join(cacheDir, `${data.uniqid}.content`);
|
||||||
tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);
|
||||||
@ -184,13 +187,27 @@ const server = http.createServer(async (req, res) => {
|
|||||||
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
fetchAndServe(data, tempCacheContentFile, cacheContentFile, cacheMetaFile, res);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
// 记录响应错误
|
||||||
|
viewsInfo.fetchApiError++;
|
||||||
|
|
||||||
// utf8 解码
|
// utf8 解码
|
||||||
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
|
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
|
||||||
res.end(apiData.message || 'Bad Gateway');
|
res.end(apiData.message || 'Bad Gateway');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8' });
|
// 记录响应错误
|
||||||
res.end('Bad Gateway: Failed to decode JSON ' + error);
|
viewsInfo.fetchApiError++;
|
||||||
|
|
||||||
|
// 如果有缓存有忽略过期,直接调用
|
||||||
|
if (pathIndex[uniqidhex] && fs.existsSync(cacheMetaFile) && fs.existsSync(cacheContentFile)) {
|
||||||
|
serveFromCache(JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8')), cacheContentFile, cacheMetaFile, res);
|
||||||
|
} else {
|
||||||
|
// utf8 解码
|
||||||
|
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8;' });
|
||||||
|
res.end('Bad Gateway: Failed to decode JSON' + error);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -357,6 +374,11 @@ const serveFromCache = (cacheData, cacheContentFile, cacheMetaFile, res) => {
|
|||||||
|
|
||||||
// 处理响应错误
|
// 处理响应错误
|
||||||
const handleResponseError = (res, tempCacheContentFile, realUrl) => {
|
const handleResponseError = (res, tempCacheContentFile, realUrl) => {
|
||||||
|
|
||||||
|
|
||||||
|
// 增加缓存读取错误次数
|
||||||
|
viewsInfo.cacheReadError++;
|
||||||
|
|
||||||
if (!res.headersSent) {
|
if (!res.headersSent) {
|
||||||
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8' });
|
res.writeHead(502, { 'Content-Type': 'text/plain;charset=UTF-8' });
|
||||||
res.end(`Bad Gateway: ${realUrl}`);
|
res.end(`Bad Gateway: ${realUrl}`);
|
||||||
@ -368,6 +390,10 @@ const handleResponseError = (res, tempCacheContentFile, realUrl) => {
|
|||||||
|
|
||||||
// 处理缓存读取错误
|
// 处理缓存读取错误
|
||||||
const handleCacheReadError = (res) => {
|
const handleCacheReadError = (res) => {
|
||||||
|
|
||||||
|
// 增加缓存读取错误次数
|
||||||
|
viewsInfo.cacheReadError++;
|
||||||
|
|
||||||
if (!res.headersSent) {
|
if (!res.headersSent) {
|
||||||
res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
|
res.writeHead(500, { 'Content-Type': 'text/plain;charset=UTF-8' });
|
||||||
res.end('Internal Server Error: Unable to read cache content file');
|
res.end('Internal Server Error: Unable to read cache content file');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user