From aed0ad163f664eff7b4c14c794297461dafd5728 Mon Sep 17 00:00:00 2001 From: wangshouren <1115808717@qq.com> Date: Mon, 8 Jul 2024 07:57:47 +0800 Subject: [PATCH] socket server --- .dockerignore | 8 ++ Dockerfile | 75 +++++++++++++++++++ apps/large-file-upload/app/layout.tsx | 1 + .../libs/socket/start-socket-server.ts | 42 +++++++++++ apps/large-file-upload/package.json | 8 +- docker-compose.yml | 23 ++++++ 6 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 apps/large-file-upload/libs/socket/start-socket-server.ts create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0b09fcf --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +Dockerfile +.dockerignore +node_modules +npm-debug.log +README.md +.next +.git +.turbo \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..06185ba --- /dev/null +++ b/Dockerfile @@ -0,0 +1,75 @@ +FROM node:18-alpine AS base + + +# Install dependencies only when needed +FROM base AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +RUN npm i nrm -g && nrm use taobao +RUN npm i http-server -g + +WORKDIR /app + +# Install dependencies based on the preferred package manager +COPY package.json pnpm-workspace.yaml pnpm-lock.yaml ./ +COPY ./apps/stackoverflow/package.json ./apps/stackoverflow/package.json +COPY ./apps/large-file-upload/package.json ./apps/large-file-upload/package.json +RUN corepack enable pnpm && pnpm i --frozen-lockfile --ignore-scripts + +# Rebuild the source code only when needed +FROM deps AS builder +WORKDIR /app +COPY . . +# Post install scripts +RUN pnpm i --frozen-lockfile + +# Uncomment and use build args to enable remote caching +ENV TURBO_TEAM=xiaosen7s-projects +ENV TURBO_TOKEN=rIuM8sPR68dOokTcnVr6YiJr +ENV TURBO_REMOTE_ONLY=true + +RUN pnpm turbo check-types + +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry during the build. +# ENV NEXT_TELEMETRY_DISABLED 1 + +# RUN \ +# if [ -f yarn.lock ]; then yarn run build; \ +# elif [ -f package-lock.json ]; then npm run build; \ +# elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ +# else echo "Lockfile not found." && exit 1; \ +# fi + +# # Production image, copy all the files and run next +# FROM base AS runner +# WORKDIR /app + +# ENV NODE_ENV production +# # Uncomment the following line in case you want to disable telemetry during runtime. +# # ENV NEXT_TELEMETRY_DISABLED 1 + +# RUN addgroup --system --gid 1001 nodejs +# RUN adduser --system --uid 1001 nextjs + +# COPY --from=builder /app/public ./public + +# # Set the correct permission for prerender cache +# RUN mkdir .next +# RUN chown nextjs:nodejs .next + +# # Automatically leverage output traces to reduce image size +# # https://nextjs.org/docs/advanced-features/output-file-tracing +# COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +# COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +# USER nextjs + +# EXPOSE 3000 + +# ENV PORT 3000 + +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/next-config-js/output +CMD http-server \ No newline at end of file diff --git a/apps/large-file-upload/app/layout.tsx b/apps/large-file-upload/app/layout.tsx index 6fb709f..d1525dc 100644 --- a/apps/large-file-upload/app/layout.tsx +++ b/apps/large-file-upload/app/layout.tsx @@ -1,5 +1,6 @@ import { GitLog } from "@/shared/components/git-log"; import { cn } from "@/shared/utils"; +// import "@/socket/start-socket-server"; import type { Metadata } from "next"; import { Inter } from "next/font/google"; import "./globals.css"; diff --git a/apps/large-file-upload/libs/socket/start-socket-server.ts b/apps/large-file-upload/libs/socket/start-socket-server.ts new file mode 100644 index 0000000..738e163 --- /dev/null +++ b/apps/large-file-upload/libs/socket/start-socket-server.ts @@ -0,0 +1,42 @@ +import { uploadActions } from "@/actions/upload"; +import { WEBSOCKET_PORT } from "@/shared/constants"; +import { SocketServer } from "@/socket/models/server"; +import { DEFAULTS } from "@/upload/constants/defaults"; +import { createServer } from "node:http"; +import { Server } from "socket.io"; + +function createSocketServer() { + const hostname = "0.0.0.0"; + + const httpServer = createServer(); + + const server = new SocketServer( + new Server(httpServer, { + maxHttpBufferSize: DEFAULTS.maxChunkSize, + cors: { + origin: "*", + }, + }), + uploadActions + ); + + httpServer + .once("error", (err) => { + console.error(err); + process.exit(1); + }) + .listen(WEBSOCKET_PORT, () => { + console.log( + `> Socket server is ready on http://${hostname}:${WEBSOCKET_PORT}` + ); + }); + + return server; +} + +// @ts-ignore +if (!global.socketServer) { + console.log("Creating socket server..."); + // @ts-ignore + global.socketServer = createSocketServer(); +} diff --git a/apps/large-file-upload/package.json b/apps/large-file-upload/package.json index 99f8f73..678b83d 100644 --- a/apps/large-file-upload/package.json +++ b/apps/large-file-upload/package.json @@ -3,12 +3,8 @@ "version": "0.1.0", "private": true, "scripts": { - "dev": "run-p dev:next dev:socket-server", - "build": "run-p build:next", - "dev:next": "next dev", - "dev:socket-server": "tsx watch -r tsconfig-paths/register socket-server.ts", - "build:next": "next build", - "build:socket-server": "tsup socket-server.ts", + "dev": "next dev", + "build": "next build", "lint": "next lint", "check-types": "npx tsc --noEmit", "test": "vitest --run" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..50d4080 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +services: + postgres: + image: postgres:16 + restart: always + shm_size: 128mb + environment: + POSTGRES_PASSWORD: 123456 + + adminer: + image: adminer + restart: always + ports: + - 8080:8080 + + app: + build: . + restart: always + # environment: + # - TURBO_TOKEN=rIuM8sPR68dOokTcnVr6YiJr + # - TURBO_TEAM=xiaosen7s-projects + # - TURBO_REMOTE_ONLY=true + depends_on: + - postgres