From 1cddf72f1d709d76ecebbc82178d0c982ed1c4ea Mon Sep 17 00:00:00 2001 From: Andy Payne Date: Fri, 2 Aug 2024 16:13:43 +0100 Subject: [PATCH] feat: update Dockerfile and middleware routing --- Dockerfile | 38 ++++++++++++++++---------------------- middleware.ts | 9 +++++++++ server.js | 23 ++++++++--------------- 3 files changed, 33 insertions(+), 37 deletions(-) create mode 100644 middleware.ts diff --git a/Dockerfile b/Dockerfile index 8942118..08055ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,27 @@ -FROM node:18.18-alpine AS deps -# RUN apk add --no-cache libc6-compat gcompat -# RUN apk add --no-cache g++ make +# Stage 1: Build +FROM node:18.18-alpine AS builder +# Set working directory WORKDIR /app -COPY package.json package-lock.json ./ -RUN --mount=type=secret,id=FONTAWESOME_NPM_AUTH_TOKEN \ - npm config set "@fortawesome:registry" https://npm.fontawesome.com/ && \ - export FONTAWESOME_NPM_AUTH_TOKEN=$(cat /run/secrets/FONTAWESOME_NPM_AUTH_TOKEN) && \ - npm config set "//npm.fontawesome.com/:_authToken" $FONTAWESOME_NPM_AUTH_TOKEN && \ - npm install && \ - echo "done" - # find /app/node_modules/ ! -user root | xargs chown root:root - +# Copy package.json and package-lock.json +COPY package*.json ./ -FROM node:18.18-alpine AS builder -# RUN apk add --no-cache libc6-compat gcompat -# RUN apk add --no-cache g++ make +# Install dependencies +RUN npm install -# RUN ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2 -WORKDIR /app -COPY --from=deps /app/node_modules ./node_modules +# Copy all project files COPY . . ENV NEXT_TELEMETRY_DISABLED 1 +# Build the application RUN npm run build +# Stage 2: Run FROM node:18.18-alpine AS runner +# Set working directory WORKDIR /app ENV NODE_ENV production @@ -37,16 +30,17 @@ ENV NEXT_TELEMETRY_DISABLED 1 RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs +# Copy only the necessary files from the build stage +COPY --from=builder /app/package*.json ./ COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next - COPY --from=builder /app/node_modules ./node_modules -COPY --from=builder /app/package.json ./package.json COPY --from=builder /app/server.js ./server.js +COPY --from=builder /app/middleware.ts ./ +# Expose the port the app runs on USER nextjs EXPOSE 3000 -ENV PORT 3000 - +# Command to run the app CMD ["node", "server.js"] diff --git a/middleware.ts b/middleware.ts new file mode 100644 index 0000000..db98561 --- /dev/null +++ b/middleware.ts @@ -0,0 +1,9 @@ +import type { NextRequest } from 'next/server'; +import { NextResponse } from 'next/server'; + +export function middleware(request: NextRequest) { + if (request.nextUrl.pathname.startsWith('/resourcing')) { + return NextResponse.rewrite(new URL('/', request.url)); + } + return NextResponse.next(); +} diff --git a/server.js b/server.js index 14b2877..705249d 100644 --- a/server.js +++ b/server.js @@ -25,28 +25,22 @@ const handler = app.getRequestHandler(); const nextUpgradeHandler = app.getUpgradeHandler(); app.prepare().then(() => { - const httpServer = createServer((req, res) => { - const parsedUrl = parse(req.url, true); - const { pathname } = parsedUrl; - - if (pathname.startsWith('/resourcing')) { - // Rewrite /resourcing to / - const newUrl = req.url.replace('/resourcing', ''); - req.url = newUrl; - app.render(req, res, '/', parsedUrl.query); - } else { - handler(req, res, parsedUrl); - } - }); + const httpServer = createServer(handler); httpServer .once('error', (err) => { console.error(err); process.exit(1); }) + // eslint-disable-next-line consistent-return .on('upgrade', (request, socket, head) => { + // You may check auth of request here.. + // See https://github.com/websockets/ws#client-authentication + /** + * @param {any} ws + */ const { pathname } = parse(request.url || '/', true); - + // Make sure we all for hot module reloading if (pathname === '/_next/webpack-hmr') { return nextUpgradeHandler(request, socket, head); } @@ -55,7 +49,6 @@ app.prepare().then(() => { wss.emit('connection', ws, request); }; wss.handleUpgrade(request, socket, head, handleAuth); - return undefined; // Explicitly return undefined to satisfy consistent-return }) .listen(port, () => { console.log(`> Ready on http://${hostname}:${port}`);