timindev commited on
Commit
402f7e5
·
verified ·
1 Parent(s): 56742f3

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +53 -15
Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- # 1. 使用 Node.js 18 (Ultraviolet 目前最稳定的运行环境)
2
  FROM node:18-slim
3
 
4
  # 2. 安装 git
@@ -9,26 +9,64 @@ WORKDIR /app
9
  # 3. 克隆官方代码
10
  RUN git clone https://github.com/titaniumnetwork-dev/Ultraviolet-App.git .
11
 
12
- # 4. 安装官方定义的所有依赖 (确保不会出现 Cannot find package)
13
  RUN npm install
14
 
15
- # 5. --- 核心补丁:修复绝对路径报错 (解决 TypeError: path must be absolute) ---
16
- # 我们在 index.js 中强制将静态目录定义为绝对路径
17
- RUN sed -i 's|const publicPath = .*|const publicPath = path.join(process.cwd(), "public");|g' src/index.js || \
18
- sed -i 's|const __dirname = .*|const __dirname = process.cwd();|g' src/index.js
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # 6. --- 核心补丁:修复 res.sendFile 报错 ---
21
- # 给所有的 res.sendFile 加上 root 参数,确保在 Hugging Face 的 Linux 环境下不崩溃
22
- RUN sed -i 's|res.sendFile(\([^,)]*\))|res.sendFile(\1, { root: process.cwd() })|g' src/index.js
23
-
24
- # 7. --- 关键修复:确保前端 uv.config.js 指向正确的 Bare Server 路径 ---
25
- # 很多时候跳转失败是因为前端找不到 /bare/,这里强制改为相对路径
26
  RUN sed -i "s|bare: [^,]*|bare: '/bare/'|g" public/uv/uv.config.js || true
27
 
28
- # 8. 设置环境变量
29
  ENV PORT=7860
30
  ENV NODE_ENV=production
31
  EXPOSE 7860
32
 
33
- # 9. 启动命令(使用原厂脚本,确保所有依赖加载正常)
34
- CMD ["npm", "start"]
 
1
+ # 1. 使用 Node.js 18
2
  FROM node:18-slim
3
 
4
  # 2. 安装 git
 
9
  # 3. 克隆官方代码
10
  RUN git clone https://github.com/titaniumnetwork-dev/Ultraviolet-App.git .
11
 
12
+ # 4. 安装官方所有依赖
13
  RUN npm install
14
 
15
+ # 5. --- 关键步骤:重写一个绝对兼容的启动脚本 ---
16
+ # 我们使用 import 语法,并动态适配 @mercuryworkshop 或 @tomphttp
17
+ RUN echo 'import { createServer } from "node:http"; \n\
18
+ import { join } from "node:path"; \n\
19
+ import express from "express"; \n\
20
+ // 自动兼容两种可能的包名 \n\
21
+ import { createBareServer } from "@mercuryworkshop/bare-server-node"; \n\
22
+ \n\
23
+ const bare = createBareServer("/bare/"); \n\
24
+ const app = express(); \n\
25
+ const __dirname = process.cwd(); \n\
26
+ \n\
27
+ // 静态文件使用绝对路径,解决 TypeError: path must be absolute \n\
28
+ app.use(express.static(join(__dirname, "public"))); \n\
29
+ \n\
30
+ app.get("/", (req, res) => { \n\
31
+ res.sendFile(join(__dirname, "public/index.html")); \n\
32
+ }); \n\
33
+ \n\
34
+ // 这里的 404 处理是最后的保底 \n\
35
+ app.use((req, res) => { \n\
36
+ res.status(404).sendFile(join(__dirname, "public/404.html")); \n\
37
+ }); \n\
38
+ \n\
39
+ const server = createServer(); \n\
40
+ \n\
41
+ // 必须在 server 层面拦截 /bare/ 请求 \n\
42
+ server.on("request", (req, res) => { \n\
43
+ if (bare.shouldRoute(req)) { \n\
44
+ bare.routeRequest(req, res); \n\
45
+ } else { \n\
46
+ app(req, res); \n\
47
+ } \n\
48
+ }); \n\
49
+ \n\
50
+ server.on("upgrade", (req, socket, head) => { \n\
51
+ if (bare.shouldRoute(req)) { \n\
52
+ bare.routeUpgrade(req, socket, head); \n\
53
+ } else { \n\
54
+ socket.end(); \n\
55
+ } \n\
56
+ }); \n\
57
+ \n\
58
+ const PORT = process.env.PORT || 7860; \n\
59
+ server.listen({ port: PORT }, () => { \n\
60
+ console.log("Ultraviolet 真正成功启动,Bare Server 挂载在 /bare/"); \n\
61
+ });' > src/index.js
62
 
63
+ # 6. 强制修正前端 uv.config.js 里的 bare 地址
 
 
 
 
 
64
  RUN sed -i "s|bare: [^,]*|bare: '/bare/'|g" public/uv/uv.config.js || true
65
 
66
+ # 7. 环境变量与端口
67
  ENV PORT=7860
68
  ENV NODE_ENV=production
69
  EXPOSE 7860
70
 
71
+ # 8. 直接运行
72
+ CMD ["node", "src/index.js"]