feat(Dockerfile): 添加Dockerfile支持容器化部署

build(fastify.js): 更新API基础地址并增强fetchApi功能

修改API基础地址为183.6.121.121:9558/api
扩展fetchApi函数支持查询参数传递
优化缓存逻辑,增加nocache条件判断
This commit is contained in:
2026-01-06 18:50:18 +08:00
parent 81883761ce
commit cb4076bdc0
2 changed files with 45 additions and 8 deletions

34
Dockerfile Normal file
View File

@@ -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"]

View File

@@ -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(() => { });
}