From 441aa641663c0fefda173bd8952f2af6d408e05b Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Sun, 23 Mar 2025 22:01:13 +0100 Subject: [PATCH] feat: docker stuff --- .github/workflows/docker.yml | 37 +++++++++++++++++++++++++++++- .gitignore | 3 ++- apps/chat/Dockerfile | 43 +++++++++++++++++++++++++++++++++++ apps/chat/package.json | 6 +++-- apps/chat/src/index.ts | 4 ++++ apps/chat/tsconfig.json | 2 ++ apps/web/next.config.mjs | 2 +- packages/hono-ws/src/index.ts | 3 ++- turbo.json | 2 +- 9 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 apps/chat/Dockerfile diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 417d441..d0b7286 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -76,10 +76,45 @@ jobs: secrets: | TURBO_TOKEN=${{ secrets.TURBO_TOKEN }} TURBO_TEAM=${{ secrets.TURBO_TEAM }} + chat: + name: Push chat module to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: srizan10/hclive-chat + tags: latest + + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: . + file: ./apps/chat/Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64 + secrets: | + TURBO_TOKEN=${{ secrets.TURBO_TOKEN }} + TURBO_TEAM=${{ secrets.TURBO_TEAM }} deploy: name: Deploy to server runs-on: ubuntu-latest - needs: [frontend, db] + needs: [frontend, db, chat] steps: - name: Emit a webhook to the server env: diff --git a/.gitignore b/.gitignore index 693295d..7338ee4 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,5 @@ certificates dev/psql .turbo -packages/db/generated/client \ No newline at end of file +packages/db/generated/client +*dist \ No newline at end of file diff --git a/apps/chat/Dockerfile b/apps/chat/Dockerfile new file mode 100644 index 0000000..e909d2a --- /dev/null +++ b/apps/chat/Dockerfile @@ -0,0 +1,43 @@ +FROM node:lts-alpine AS base + +FROM base AS builder +RUN apk update +RUN apk add --no-cache libc6-compat +WORKDIR /app +RUN yarn global add turbo@^2 +COPY . . + +RUN turbo prune @hctv/chat --docker + +FROM base AS installer +RUN apk update +RUN apk add --no-cache libc6-compat +WORKDIR /app + +# First install the dependencies +COPY --from=builder /app/out/json/ . +RUN yarn install --frozen-lockfile + +COPY --from=builder /app/out/full/ . +RUN --mount=type=secret,id=TURBO_TOKEN --mount=type=secret,id=TURBO_TEAM TURBO_TOKEN=$(cat /run/secrets/TURBO_TOKEN) TURBO_TEAM=$(cat /run/secrets/TURBO_TEAM) yarn turbo run build --filter=@hctv/chat + +FROM base AS runner +WORKDIR /app + +RUN apk add --no-cache curl + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nodeapp +USER nodeapp + +COPY --from=installer --chown=nodeapp:nodejs /app/apps/chat ./apps/chat +COPY --from=installer --chown=nodeapp:nodejs /app/node_modules ./node_modules +COPY --from=installer --chown=nodeapp:nodejs /app/package.json ./package.json + +ENV NODE_ENV=production + +WORKDIR /app/apps/chat + +EXPOSE 8000 + +ENTRYPOINT ["node", "dist/index.js"] \ No newline at end of file diff --git a/apps/chat/package.json b/apps/chat/package.json index 2414277..b7c90a6 100644 --- a/apps/chat/package.json +++ b/apps/chat/package.json @@ -3,7 +3,8 @@ "version": "0.1.0", "type": "module", "scripts": { - "dev": "tsx watch src/index.ts" + "dev": "tsx watch src/index.ts", + "build": "tsc --build" }, "dependencies": { "@hctv/auth": "*", @@ -16,6 +17,7 @@ }, "devDependencies": { "@types/node": "^20.11.17", - "tsx": "^4.7.1" + "tsx": "^4.7.1", + "typescript": "^5.8.2" } } diff --git a/apps/chat/src/index.ts b/apps/chat/src/index.ts index a46987e..330e56d 100644 --- a/apps/chat/src/index.ts +++ b/apps/chat/src/index.ts @@ -16,6 +16,10 @@ app.get('/', async (c) => { return c.text(threed); }); +app.get('/up', async (c) => { + return c.text('it works'); +}); + app.get( '/ws/:username', upgradeWebSocket((c) => ({ diff --git a/apps/chat/tsconfig.json b/apps/chat/tsconfig.json index 7719c23..0c1ea46 100644 --- a/apps/chat/tsconfig.json +++ b/apps/chat/tsconfig.json @@ -10,5 +10,7 @@ ], "jsx": "react-jsx", "jsxImportSource": "hono/jsx", + "outDir": "dist", + "rootDir": "src" } } \ No newline at end of file diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs index 80effe3..d3812f8 100644 --- a/apps/web/next.config.mjs +++ b/apps/web/next.config.mjs @@ -29,7 +29,7 @@ const nextConfig = { return [ { source: '/api/stream/chat/:path*', - destination: `http://localhost:8000/:path*`, + destination: `http://${process.env.NODE_ENV === 'production' ? 'chat' : 'localhost'}:8000/:path*`, }, ]; } diff --git a/packages/hono-ws/src/index.ts b/packages/hono-ws/src/index.ts index 4b21d74..d2253f4 100644 --- a/packages/hono-ws/src/index.ts +++ b/packages/hono-ws/src/index.ts @@ -5,6 +5,7 @@ import type { IncomingMessage } from 'http'; import type { Server } from 'node:http'; import type { Http2SecureServer, Http2Server } from 'node:http2'; import type { Duplex } from 'node:stream'; +import type { Buffer } from 'node:buffer'; import type { Channel, User } from '@hctv/db'; /** @@ -214,7 +215,7 @@ export declare class WSContext { protocol: string | null; close(code?: number, reason?: string): void; } -export type WSMessageReceive = string | Blob | ArrayBufferLike; +export type WSMessageReceive = string | Blob | ArrayBufferLike | Buffer; export declare const createWSMessageEvent: ( source: WSMessageReceive ) => MessageEvent; diff --git a/turbo.json b/turbo.json index b65650e..227ab66 100644 --- a/turbo.json +++ b/turbo.json @@ -3,7 +3,7 @@ "tasks": { "build": { "dependsOn": ["^db:generate", "prepare"], - "outputs": [".next/**", "!.next/cache/**"] + "outputs": [".next/**", "!.next/cache/**", "dist/**"] }, "setup": { "dependsOn": ["^dd", "^db:generate", "^build"],