timindev commited on
Commit
151c8a9
·
verified ·
1 Parent(s): 4a77ac6

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +56 -30
Dockerfile CHANGED
@@ -1,42 +1,68 @@
1
- # 使用轻量级的 Node.js 镜像
2
- FROM node:18-slim
3
 
4
- # 安装 git 以克隆代码
5
  RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
6
 
7
- # 克隆 Ultraviolet 的官方精简部署版
8
- RUN git clone https://github.com/titaniumnetwork-dev/Ultraviolet-App.git /app
9
-
10
  WORKDIR /app
11
 
12
- # 安装依赖
13
- RUN npm install
14
 
15
- # --- 核心修复:注入全局底层补丁,优雅修复 Express 的相对路径限制 ---
16
- RUN cat << 'EOF' > patch.js
17
- const fs = require('fs');
18
- const patchCode = `
19
- import express from 'express';
20
- import path from 'path';
21
- const originalSendFile = express.response.sendFile;
22
- express.response.sendFile = function(pathName, options, callback) {
23
- if (typeof pathName === 'string' && !path.isAbsolute(pathName)) {
24
- pathName = path.resolve(process.cwd(), pathName);
25
- }
26
- return originalSendFile.call(this, pathName, options, callback);
27
- };
28
- `;
29
- const data = fs.readFileSync('src/index.js', 'utf8');
30
- fs.writeFileSync('src/index.js', patchCode + data);
31
- EOF
32
 
33
- # 运行补丁脚本并清理临时文件
34
- RUN node patch.js && rm patch.js
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- # 设置 Hugging Face 必须的 7860 端口
37
  ENV PORT=7860
38
  ENV NODE_ENV=production
39
  EXPOSE 7860
40
 
41
- # 启动程序
42
- CMD ["npm", "start"]
 
1
+ # 1. 使用更高版本的 Node(项目要求 24+,这里用 22-slim 是目前最稳妥的)
2
+ FROM node:22-slim
3
 
4
+ # 2. 安装 git
5
  RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
6
 
 
 
 
7
  WORKDIR /app
8
 
9
+ # 3. 克隆代码
10
+ RUN git clone https://github.com/titaniumnetwork-dev/Ultraviolet-App.git .
11
 
12
+ # 4. 安装依赖
13
+ RUN npm install
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # 5. --- 彻底修复:直接重写 src/index.js ---
16
+ # 这里使用符合 ES Module (import) 规范的代码,并严格修正路径
17
+ RUN echo "import { createServer } from 'node:http'; \n\
18
+ import { join } from 'node:path'; \n\
19
+ import express from 'express'; \n\
20
+ import { createBareServer } from '@tomphttp/bare-server-node'; \n\
21
+ \n\
22
+ const bare = createBareServer('/bare/'); \n\
23
+ const app = express(); \n\
24
+ const __dirname = process.cwd(); \n\
25
+ const publicPath = join(__dirname, 'public'); \n\
26
+ \n\
27
+ // 静态文件处理 \n\
28
+ app.use(express.static(publicPath)); \n\
29
+ \n\
30
+ app.get('/', (req, res) => { \n\
31
+ res.sendFile(join(publicPath, 'index.html')); \n\
32
+ }); \n\
33
+ \n\
34
+ // 统一处理 404,防止路径拼错 \n\
35
+ app.use((req, res) => { \n\
36
+ res.status(404).sendFile(join(publicPath, '404.html')); \n\
37
+ }); \n\
38
+ \n\
39
+ const server = createServer(); \n\
40
+ \n\
41
+ server.on('request', (req, res) => { \n\
42
+ if (bare.shouldRoute(req)) { \n\
43
+ bare.routeRequest(req, res); \n\
44
+ } else { \n\
45
+ app(req, res); \n\
46
+ } \n\
47
+ }); \n\
48
+ \n\
49
+ server.on('upgrade', (req, socket, head) => { \n\
50
+ if (bare.shouldRoute(req)) { \n\
51
+ bare.routeUpgrade(req, socket, head); \n\
52
+ } else { \n\
53
+ socket.end(); \n\
54
+ } \n\
55
+ }); \n\
56
+ \n\
57
+ const PORT = process.env.PORT || 7860; \n\
58
+ server.listen({ port: PORT }, () => { \n\
59
+ console.log('Server is running on port', PORT); \n\
60
+ });" > src/index.js
61
 
62
+ # 6. 设置环境变量
63
  ENV PORT=7860
64
  ENV NODE_ENV=production
65
  EXPOSE 7860
66
 
67
+ # 7. 启动
68
+ CMD ["node", "src/index.js"]