Skip to content

Commit

Permalink
feat: remove postgres & use a single docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Oct 11, 2022
1 parent e2b3e6a commit 388ac39
Show file tree
Hide file tree
Showing 18 changed files with 350 additions and 275 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
backend/dist/
backend/node_modules/

frontend/node_modules/
frontend/.next/
frontend/dist/

**/.git/
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM node:18-alpine AS frontend-builder
WORKDIR /opt/app
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci
COPY ./frontend .
RUN npm run build

FROM node:18 AS backend-builder
WORKDIR /opt/app
COPY backend/package.json backend/package-lock.json ./
RUN npm ci
COPY ./backend .
RUN npx prisma generate
RUN npm run build





FROM node:18 AS runner
WORKDIR /opt/app/frontend
ENV NODE_ENV=production
COPY --from=frontend-builder /opt/app/next.config.js .
COPY --from=frontend-builder /opt/app/public ./public
COPY --from=frontend-builder /opt/app/.next ./.next
COPY --from=frontend-builder /opt/app/node_modules ./node_modules

WORKDIR /opt/app/backend
COPY --from=backend-builder /opt/app/node_modules ./node_modules
COPY --from=backend-builder /opt/app/dist ./dist
COPY --from=backend-builder /opt/app/prisma ./prisma
COPY --from=backend-builder /opt/app/package.json ./
WORKDIR /opt/app

RUN npm i -g dotenv-cli

EXPOSE 3000
CMD cd frontend && dotenv -e .env.development node_modules/.bin/next start & cd backend && npm run prod
2 changes: 1 addition & 1 deletion backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/
/frontend/node_modules/
dist/
.git/
75 changes: 0 additions & 75 deletions backend/prisma/migrations/20221006161700_inital/migration.sql

This file was deleted.

57 changes: 57 additions & 0 deletions backend/prisma/migrations/20221011172612_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
"firstName" TEXT,
"lastName" TEXT
);

-- CreateTable
CREATE TABLE "RefreshToken" (
"token" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"expiresAt" DATETIME NOT NULL,
"userId" TEXT NOT NULL,
CONSTRAINT "RefreshToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Share" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"uploadLocked" BOOLEAN NOT NULL DEFAULT false,
"isZipReady" BOOLEAN NOT NULL DEFAULT false,
"views" INTEGER NOT NULL DEFAULT 0,
"expiration" DATETIME NOT NULL,
"creatorId" TEXT NOT NULL,
CONSTRAINT "Share_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "File" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"name" TEXT NOT NULL,
"size" TEXT NOT NULL,
"shareId" TEXT NOT NULL,
CONSTRAINT "File_shareId_fkey" FOREIGN KEY ("shareId") REFERENCES "Share" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ShareSecurity" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"password" TEXT,
"maxViews" INTEGER,
"shareId" TEXT,
CONSTRAINT "ShareSecurity_shareId_fkey" FOREIGN KEY ("shareId") REFERENCES "Share" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "ShareSecurity_shareId_key" ON "ShareSecurity"("shareId");
2 changes: 1 addition & 1 deletion backend/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
provider = "sqlite"
Binary file added backend/prisma/pingvin-share.db
Binary file not shown.
Binary file added backend/prisma/pingvin-share.db-journal
Binary file not shown.
6 changes: 3 additions & 3 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("DB_URL")
provider = "sqlite"
url = "file:./pingvin-share.db"
}

model User {
Expand All @@ -25,7 +25,7 @@ model RefreshToken {
token String @id @default(uuid())
createdAt DateTime @default(now())
expiresAt DateTime @default(dbgenerated("NOW() + interval '3 months'"))
expiresAt DateTime
userId String
user User @relation(fields: [userId], references: [id])
Expand Down
6 changes: 4 additions & 2 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
import { ConfigService } from "@nestjs/config";
import { JwtService } from "@nestjs/jwt";
import { User } from "@prisma/client";

import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
import * as argon from "argon2";
import * as moment from "moment";
import { PrismaService } from "src/prisma/prisma.service";
import { AuthRegisterDTO } from "./dto/authRegister.dto";
import { AuthSignInDTO } from "./dto/authSignIn.dto";
Expand Down Expand Up @@ -87,7 +87,9 @@ export class AuthService {

async createRefreshToken(userId: string) {
const refreshToken = (
await this.prisma.refreshToken.create({ data: { userId } })
await this.prisma.refreshToken.create({
data: { userId, expiresAt: moment().add(3, "months").toDate() },
})
).token;

return refreshToken;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class PrismaService extends PrismaClient {
super({
datasources: {
db: {
url: config.get("DB_URL"),
url: "file:./pingvin-share.db",
},
},
});
Expand Down
1 change: 1 addition & 0 deletions data/pingvin-share.db
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This will be your database, it must exist because Docker can't mount the volume if it doesn't exist.
41 changes: 7 additions & 34 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,16 @@
version: '3.8'
services:
db:
image: postgres:14.1-alpine
restart: unless-stopped
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=pingvin-share
volumes:
- pingvin-share-db:/var/lib/postgresql/data
backend:
image: stonith404/pingvin-share-backend
pingvin-share:
image: stonith404/pingvin-share
restart: unless-stopped
ports:
- 3000:3000
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- DB_HOST=${DB_HOST}
- DB_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}/pingvin-share?schema=public
- APP_URL=${APP_URL}
- SHOW_HOME_PAGE=${SHOW_HOME_PAGE}
- ALLOW_REGISTRATION=${ALLOW_REGISTRATION}
- MAX_FILE_SIZE=${MAX_FILE_SIZE}
- JWT_SECRET=${JWT_SECRET}
depends_on:
- db
volumes:
- "./uploads:/usr/src/app/uploads"
frontend:
restart: unless-stopped
ports:
- '3000:3000'
image: stonith404/pingvin-share-frontend
environment:
- SHOW_HOME_PAGE=${SHOW_HOME_PAGE}
- ALLOW_REGISTRATION=${ALLOW_REGISTRATION}
- MAX_FILE_SIZE=${MAX_FILE_SIZE}
- BACKEND_URL=${BACKEND_URL}
depends_on:
- backend


volumes:
pingvin-share-db:
- "${PWD}/data/uploads:/opt/app/backend/uploads"
- "${PWD}/data/pingvin-share.db:/opt/app/backend/prisma/pingvin-share.db"
3 changes: 1 addition & 2 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM node:18-alpine AS deps
WORKDIR /opt/app
COPY package.json package-lock.json ./
RUN npm ci


FROM node:18-alpine AS builder
ENV NODE_ENV=production
Expand Down
Loading

0 comments on commit 388ac39

Please sign in to comment.