Skip to content

Commit

Permalink
Merge pull request #102 from CS3219-AY2324S1/docker-test
Browse files Browse the repository at this point in the history
Containerization
  • Loading branch information
Nafeij authored Nov 12, 2023
2 parents ddc9e82 + 6ff332c commit 7d30e52
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 209 deletions.
15 changes: 12 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/node_modules
/.next
/npm-debug.log
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.server
data
docs
judge0-*
.git
/.env
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ NEXTAUTH_SECRET=NEXTAUTH_SECRET
NEXT_AUTH_SECRET=EXAMPLE_JWT_SECRET
NEXTAUTH_URL="http://localhost:${PORT}"

# Judge0
J0_URL="http://localhost:2358"

# S3
S3_ENDPOINT="http://localhost:9000"
S3_BUCKET_NAME=s3

# Github OAuth Provider
GITHUB_ID=EXAMPLE_GITHUB_ID
GITHUB_SECRET=EXAMPLE_GITHUB_SECRET
GITHUB_SECRET=EXAMPLE_GITHUB_SECRET

# OpenAI
OPENAI_API_KEY=EXAMPLE_OPENAI_ID
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Dockerfile

FROM node:18-alpine
WORKDIR /app

COPY . .
COPY /production/.env .env

RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
else echo "Lockfile not found." && exit 1; \
fi

# ARG DATABASE_URL
# ARG NEXT_PUBLIC_WS_PORT

ENV NEXT_TELEMETRY_DISABLED 1

RUN \
if [ -f yarn.lock ]; then SKIP_ENV_VALIDATION=1 yarn build; \
elif [ -f package-lock.json ]; then SKIP_ENV_VALIDATION=1 npm run build; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && SKIP_ENV_VALIDATION=1 pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

USER nextjs
EXPOSE 3000
ENV PORT 3000

CMD ["yarn", "start"]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ Notes:
- If postgres is running on 5432, kill the process `sudo pkill -u postgres`
- If you have trouble starting the `judge0` container, or encounter `Incorrect type. Expected "include".` in `compose.yml`, try updating `docker-compose` to `v2.20` (Docker Desktop `4.42.1`) or later.

## Building and Running Image

```bash
docker build -t peer-prep .
docker compose --profile prod up
```

## What's next? How do I make an app with this?

We try to keep this project as simple as possible, so you can start with just the scaffolding we set up for you, and add additional things later when they become necessary.
Expand Down
14 changes: 13 additions & 1 deletion compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
include:
- judge0-v1.13.0/docker-compose.yml
services:
app:
platform: "linux/amd64"
build:
context: .
dockerfile: Dockerfile
working_dir: /app
ports:
- "3000:3000"
- ${NEXT_PUBLIC_WS_PORT}:${NEXT_PUBLIC_WS_PORT}
image: t3-app
profiles:
- prod
postgres_db:
image: postgres:12.16
container_name: postgresdb-dev
Expand Down Expand Up @@ -36,6 +48,7 @@ services:
S3_BUCKET_NAME: ${S3_BUCKET_NAME}
volumes:
- "./data/s3:/home/app/public/peer-prep"
container_name: s3-bucket
rabbitmq:
image: rabbitmq:3.11.23-management-alpine
restart: always
Expand All @@ -48,7 +61,6 @@ services:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
command: sh -c "rabbitmq-plugins enable rabbitmq_web_stomp && rabbitmq-server"

networks:
- rabbitmq_go_net
networks:
Expand Down
3 changes: 2 additions & 1 deletion judge0-v1.13.0/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ x-logging:
max-size: 100m

services:
server:
j0-server:
image: judge0/judge0:1.13.0
volumes:
- ./judge0.conf:/judge0.conf:ro
Expand All @@ -17,6 +17,7 @@ services:
privileged: true
<<: *default-logging
restart: always
container_name: j0-server

workers:
image: judge0/judge0:1.13.0
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"prisma:generate": "prisma generate --schema prisma/mongodb/schema.prisma && prisma generate --schema prisma/postgres/schema.prisma",
"prisma:push": "prisma db push --schema prisma/mongodb/schema.prisma && prisma db push --schema prisma/postgres/schema.prisma",
"prisma:studio": "prisma studio --schema prisma/mongodb/schema.prisma | prisma studio --schema prisma/postgres/schema.prisma -p 5556",
"docker:up": "docker-compose up -d",
"docker:down": "docker-compose down",
"docker:up": "docker compose up -d",
"docker:down": "docker compose down",
"start-dev": "./start-dev-script.sh"
},
"dependencies": {
Expand Down
38 changes: 38 additions & 0 deletions production/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

## Node
NODE_ENV=production
PORT=3000
# Websocket port
NEXT_PUBLIC_WS_PORT=3002

# PostgreSQL
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_URL=

## MongoDB
MONGO_USER=
MONGO_PASSWORD=
MONGO_URL=

# Next Auth
# You can generate a new secret on the command line with:
# openssl rand -base64 32
# https://next-auth.js.org/configuration/options#secret
NEXTAUTH_SECRET=EXAMPLE_NEXTAUTH_SEC
NEXTAUTH_URL="http://localhost:${PORT}"

S3_ENDPOINT="http://localhost:9000"


# JUDGE0
J0_URL="http://j0-server:2358"

S3_BUCKET_NAME=some
# Next auth secret
NEXT_AUTH_SECRET=EXAMPLE_JWT_SECRET

# Github OAuth Provider
GITHUB_ID=
GITHUB_SECRET=
OPENAI_API_KEY=
14 changes: 6 additions & 8 deletions src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ export const env = createEnv({
process.env.NODE_ENV === "production"
? z.string().min(1)
: z.string().min(1).optional(),
NEXTAUTH_URL: z.preprocess(
// This makes Vercel deployments not fail if you don't set NEXTAUTH_URL
// Since NextAuth.js automatically uses the VERCEL_URL if present.
(str) => process.env.VERCEL_URL ?? str,
// VERCEL_URL doesn't include `https` so it cant be validated as a URL
process.env.VERCEL ? z.string().min(1) : z.string().url(),
),
NEXTAUTH_URL: z.string().min(1),
S3_BUCKET_NAME: z.string().min(1),
S3_ENDPOINT: z.string().min(1),
J0_URL: z.string().min(1),
// Add `.min(1) on ID and SECRET if you want to make sure they're not empty
GITHUB_ID: z.string().min(1),
GITHUB_SECRET: z.string().min(1),
Expand Down Expand Up @@ -53,10 +49,12 @@ export const env = createEnv({
MONGO_PASSWORD: process.env.MONGO_PASSWORD,
MONGO_URL: process.env.MONGO_URL,
NODE_ENV: process.env.NODE_ENV,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
NEXTAUTH_SECRET: process.env.NEXT_AUTH_SECRET,
NEXT_PUBLIC_WS_PORT: process.env.NODE_ENV === "production" ? process.env.NEXT_PUBLIC_WS_PORT : "3002",
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
S3_BUCKET_NAME: process.env.S3_BUCKET_NAME,
S3_ENDPOINT: process.env.S3_ENDPOINT,
J0_URL: process.env.J0_URL,
GITHUB_ID: process.env.GITHUB_ID,
GITHUB_SECRET: process.env.GITHUB_SECRET,
},
Expand Down
25 changes: 7 additions & 18 deletions src/server/api/routers/answer.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
import { z } from "zod";
import { createTRPCRouter, protectedProcedure } from "../trpc";
import {
PrismaMongoT,
prismaMongo,
prismaPostgres,
PrismaPostgresT,
} from "~/server/db";
import type { AnswerResult } from "@prisma-db-psql/client";
import type { Answer } from "@prisma-db-mongo/client";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { prismaMongo, prismaPostgres } from "~/server/db";
import { appRouter } from "../root";
import { Session } from "next-auth";
import { api } from "~/utils/api";
import axios from "axios";

import { createTRPCRouter, protectedProcedure } from "../trpc";

const answerRouter = createTRPCRouter({
getUserSubmissions: protectedProcedure.query(async ({ ctx }) => {
const user = ctx.session.user;
// update all the remaining submissions
const temporarySubmissions = await prismaPostgres.submission.findMany({
where: {
userId: ctx.session.user.id
}
userId: ctx.session.user.id,
},
});
const caller = appRouter.createCaller(ctx);
for (const tmpSubmssion of temporarySubmissions) {
await caller.judge.checkAnswer({
submissionId: tmpSubmssion.id
})
submissionId: tmpSubmssion.id,
});
}
const submissions = await prismaPostgres.questionAttempt.findMany({
where: {
Expand Down
2 changes: 1 addition & 1 deletion src/server/api/routers/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const UPLOAD_MAX_FILE_SIZE = 400000;

const s3Client = new S3Client({
region: "us-east-1",
endpoint: "http://localhost:9000",
endpoint: env.S3_ENDPOINT,
forcePathStyle: true,
credentials: {
accessKeyId: "S3RVER",
Expand Down
Loading

0 comments on commit 7d30e52

Please sign in to comment.