From cb4076bdc0867535f921a531a137279aaeed10e4 Mon Sep 17 00:00:00 2001 From: XiaoMo Date: Tue, 6 Jan 2026 18:50:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(Dockerfile):=20=E6=B7=BB=E5=8A=A0Dockerfil?= =?UTF-8?q?e=E6=94=AF=E6=8C=81=E5=AE=B9=E5=99=A8=E5=8C=96=E9=83=A8?= =?UTF-8?q?=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit build(fastify.js): 更新API基础地址并增强fetchApi功能 修改API基础地址为183.6.121.121:9558/api 扩展fetchApi函数支持查询参数传递 优化缓存逻辑,增加nocache条件判断 --- Dockerfile | 34 ++++++++++++++++++++++++++++++++++ fastify.js | 19 +++++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..eda7857 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,34 @@ +FROM mcr.microsoft.com/devcontainers/javascript-node:22 + +# Install ffmpeg for video thumbnail support +RUN apt-get update && apt-get install -y --no-install-recommends ffmpeg \ + && rm -rf /var/lib/apt/lists/* + +ENV NODE_ENV=production +WORKDIR /app + +# Use Corepack to activate pnpm from package.json's packageManager field +COPY package.json ./ +RUN corepack enable && pnpm --version && pnpm install --prod + +# Copy application sources +COPY . . + +# Run as non-root for security +RUN chown -R node:node /app +USER node + +# 定义.cache目录. 支持用户自定义 +ENV CACHE_DIR=/app/.cache + +# 确保.cache目录存在 +RUN mkdir -p $CACHE_DIR && chown -R node:node $CACHE_DIR + +# 确保.cache目录可写 +RUN chmod -R 777 $CACHE_DIR + +# Fastify listens on 9520 +EXPOSE 9520 + +# Default command +CMD ["node", "fastify.js"] diff --git a/fastify.js b/fastify.js index 74b2d0d..fd73c7a 100644 --- a/fastify.js +++ b/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(() => { }); }