Containerization

This commit is contained in:
Philipp Traber
2024-10-17 11:02:05 +02:00
parent b81eebf6c3
commit 296046fc0e
10 changed files with 110 additions and 16 deletions
+7
View File
@@ -0,0 +1,7 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
+74
View File
@@ -0,0 +1,74 @@
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
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
## WORKAROUND:
RUN rm -rf src/app/~/dash
RUN rm -rf src/app/actions
# 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 npx prisma generate
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
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]
+5 -1
View File
@@ -9,4 +9,8 @@ services:
- "5432:5432"
volumes:
- ./.vol_db:/var/lib/postgresql/data
web:
build: .
ports:
- 3000:3000
+2 -1
View File
@@ -11,7 +11,8 @@ const nextConfig = {
hostname: 'logo.clearbit.com',
}
]
}
},
output: "standalone",
};
+3 -3
View File
@@ -5,10 +5,10 @@ let prisma: PrismaClient;
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient();
} else {
if (!global.prisma) {
global.prisma = new PrismaClient();
if (!(global as any).prisma) {
(global as any).prisma = new PrismaClient();
}
prisma = global.prisma;
prisma = (global as any).prisma;
}
export default prisma;
+2 -2
View File
@@ -68,7 +68,7 @@ export async function createLinkBlock(prevState: any, formData: FormData) {
};
}
revalidatePath("/dash");
revalidatePath("/~/dash");
revalidatePath(`/~/dash/${data.data.customPageUuid}/page`);
revalidatePath(`/${customPage.page.shortcode}`);
@@ -99,7 +99,7 @@ export async function deleteLinkBlock(prevState: any, formData: FormData) {
}
});
revalidatePath("/dash");
revalidatePath("/~/dash");
return {
message: "Link block deleted"
+6 -6
View File
@@ -3,7 +3,6 @@
import { z } from "zod";
import prisma from "../../prisma/prisma";
import { revalidatePath } from "next/cache";
import { error } from "console";
export async function createCustomPage(prevState: any, fromData: FormData) {
const schema = z.object({
@@ -49,12 +48,13 @@ export async function createCustomPage(prevState: any, fromData: FormData) {
return {
message: "Shortcode already exists",
error: {
shortcode: "Shortcode already exists"
shortcode: ["Shortcode already exists"]
}
};
}
revalidatePath("/dash");
revalidatePath("/~/dash");
return {
message: "Page created",
};
@@ -93,7 +93,7 @@ export async function createRedirectPage(prevState: any, fromData: FormData) {
}
});
revalidatePath("/dash");
revalidatePath("/~/dash");
return {
message: "URL created",
@@ -141,7 +141,7 @@ export async function deletePage(prevState: any, fromData: FormData) {
}
});
revalidatePath("/dash");
revalidatePath("/~/dash");
return {
message: "Page deleted",
@@ -178,7 +178,7 @@ export async function pinPageToRoot(prevState: any, fromData: FormData) {
}
});
revalidatePath("/dash");
revalidatePath("/~/dash");
revalidatePath("/");
return {
+3 -2
View File
@@ -3,8 +3,9 @@ import { headers } from "next/headers";
import { notFound, redirect, RedirectType } from "next/navigation";
import { sha256 } from "js-sha256";
import CustomPageComponent from "@/components/CustomPageComponent";
import { PageProps } from "../../../.next/types/app/layout";
export default async function RedirectToUrl({ params }: { params: { shortcode: string } }) {
export default async function RedirectToUrl({ params }: PageProps) {
const { shortcode } = await params;
@@ -21,7 +22,7 @@ export default async function RedirectToUrl({ params }: { params: { shortcode: s
return notFound()
}
// Logg the visit
// Log the visit
const requestHeaders = await headers();
const userAgent = requestHeaders.get('user-agent') || 'unknown';
const ip = requestHeaders.get('x-forwarded-for') || 'unknown';
+7 -1
View File
@@ -8,7 +8,13 @@ import LinkFavicon from "@/components/LinkFavicon";
import { hashToColor } from "@/utils";
import PinPageToRootForm from "@/components/forms/pinPageToRootForm";
export default async function DetailedStats({ params }: { params: { uuid: string } }) {
interface PageProps {
params: Promise<{
uuid: string
}>
}
export default async function DetailedStats({ params } : PageProps) {
const { uuid } = await params;
@@ -3,6 +3,7 @@ import { createCustomPage } from "@/actions/page_actions";
import { useActionState } from "react";
import SubmitButton from "../SubmitButton";
export default function CreateCustomPageForm() {
const initialState = {