diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..94cd019 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +FROM node:18-alpine AS builder + +WORKDIR /app + +# 复制package.json和package-lock.json +COPY package*.json ./ + +# 安装依赖 +RUN npm ci + +# 复制所有源代码 +COPY . . + +# 构建前端应用 +RUN npm run build + +# 生产环境镜像 +FROM node:18-alpine + +WORKDIR /app + +# 复制构建后的前端文件 +COPY --from=builder /app/dist /app/dist + +# 复制后端文件 +COPY --from=builder /app/server /app/server +COPY --from=builder /app/package*.json /app/ + +# 只安装生产环境依赖 +RUN npm ci --only=production + +# 设置环境变量 +ENV PORT=3010 +ENV NODE_ENV=production + +# 暴露端口 +EXPOSE 3010 + +# 启动命令 +CMD ["node", "server/index.js"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6a17426 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.8' + +services: + draw-app: + build: . + container_name: draw-app + restart: always + ports: + - "3010:3010" + volumes: + - ./server/lottery.db:/app/server/lottery.db + environment: + - NODE_ENV=production + - PORT=3010 \ No newline at end of file diff --git a/server/index.js b/server/index.js index faccda2..ef6b9d3 100644 --- a/server/index.js +++ b/server/index.js @@ -11,7 +11,7 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); -const PORT = process.env.PORT || 3001; +const PORT = process.env.PORT || 3010; const server = createServer(app); const io = new SocketIOServer(server, { cors: { diff --git a/src/database/database.ts b/src/database/database.ts index 40a6959..b04d6d1 100644 --- a/src/database/database.ts +++ b/src/database/database.ts @@ -37,7 +37,7 @@ export interface Student { class DatabaseService { private static instance: DatabaseService; - private baseUrl: string = 'http://localhost:3001/api'; + private baseUrl: string = 'http://localhost:3010/api'; private constructor() { // 直接使用服务器API,无需初始化本地数据库 diff --git a/src/services/websocketService.ts b/src/services/websocketService.ts index ec50fcf..8dd3a37 100644 --- a/src/services/websocketService.ts +++ b/src/services/websocketService.ts @@ -30,10 +30,10 @@ class WebSocketService { // 获取当前主机地址和端口配置 const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const host = window.location.hostname === 'localhost' ? 'localhost' : window.location.hostname; - const port = process.env.NODE_ENV === 'production' ? window.location.port || '80' : '3001'; + const port = process.env.NODE_ENV === 'production' ? window.location.port || '80' : '3010'; const socketUrl = process.env.NODE_ENV === 'production' ? `${protocol}//${host}${port !== '80' && port !== '443' ? ':' + port : ''}` - : `${protocol}//${host}:3001`; + : `${protocol}//${host}:3010`; console.log('🔌 连接WebSocket服务器:', socketUrl, '(环境:', process.env.NODE_ENV, ')');