Skip to content

Commit

Permalink
socket server
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaosen7 committed Jul 7, 2024
1 parent af413f1 commit aed0ad1
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
.turbo
75 changes: 75 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions apps/large-file-upload/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
42 changes: 42 additions & 0 deletions apps/large-file-upload/libs/socket/start-socket-server.ts
Original file line number Diff line number Diff line change
@@ -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();
}
8 changes: 2 additions & 6 deletions apps/large-file-upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit aed0ad1

Please sign in to comment.