feat(Dockerfile): 添加Dockerfile支持容器化部署
build(fastify.js): 更新API基础地址并增强fetchApi功能 修改API基础地址为183.6.121.121:9558/api 扩展fetchApi函数支持查询参数传递 优化缓存逻辑,增加nocache条件判断
This commit is contained in:
19
fastify.js
19
fastify.js
@@ -17,7 +17,7 @@ const EventEmitter = require('events');
|
||||
|
||||
// Configuration
|
||||
const PORT = 9520;
|
||||
const API_BASE = 'http://127.0.0.1:9558/api';
|
||||
const API_BASE = 'http://183.6.121.121:9558/api';
|
||||
const CACHE_DIR = path.join(__dirname, '.cache');
|
||||
|
||||
// Ensure cache directory exists
|
||||
@@ -38,12 +38,15 @@ process.on('unhandledRejection', (reason, promise) => {
|
||||
});
|
||||
|
||||
// Helper to fetch JSON from API using Undici (Faster than http.get)
|
||||
async function fetchApi(token) {
|
||||
const apiUrl = new URL(API_BASE);
|
||||
if (token) {
|
||||
apiUrl.searchParams.set('token', token);
|
||||
}
|
||||
async function fetchApi(token, query) {
|
||||
|
||||
const apiUrl = new URL(API_BASE);
|
||||
if (query) {
|
||||
Object.entries(query).forEach(([key, value]) => {
|
||||
apiUrl.searchParams.set(key, value);
|
||||
});
|
||||
}
|
||||
|
||||
const { statusCode, body } = await request(apiUrl, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
@@ -569,7 +572,7 @@ fastify.get('/*', async (request, reply) => {
|
||||
}
|
||||
// 如果内容文件不存在,则强制刷新API,避免使用过期URL
|
||||
if (!apiData) {
|
||||
apiData = await fetchApi(token);
|
||||
apiData = await fetchApi(token, request.query);
|
||||
if (apiData.code !== 200 || !apiData.data || !apiData.data.url) {
|
||||
console.log('Invalid API response:', apiData, token);
|
||||
reply.code(404);
|
||||
@@ -585,7 +588,7 @@ fastify.get('/*', async (request, reply) => {
|
||||
}
|
||||
|
||||
const contentPath = getContentPath(apiData.data.uniqid || key);
|
||||
if (fs.existsSync(contentPath) && fs.existsSync(metaPath)) {
|
||||
if (fs.existsSync(contentPath) && fs.existsSync(metaPath) && !nocache) {
|
||||
if (isValidThumbSpec(apiData.data.thumb)) {
|
||||
return generateThumbAndCache(reply, apiData, metaPath, contentPath).catch(() => { });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user