Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google cloud run deployment #107

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ NODE_ENV=development
PORT=3000
# Websocket port
NEXT_PUBLIC_WS_PORT=3002
NEXT_PUBLIC_WS_URL="http://localhost:${PORT}"
NEXT_PUBLIC_PORT=3000

# PostgreSQL
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/db?schema=public"

MONGO_USER=
MONGO_PASSWORD=
MONGO_USER=root
MONGO_PASSWORD=password
MONGO_URL="mongodb://localhost:27017/db"

# Next Auth
Expand All @@ -29,9 +31,9 @@ J0_URL="http://localhost:2358"

S3_BUCKET_NAME=some
# Next auth secret
NEXT_AUTH_SECRET=
NEXT_AUTH_SECRET=sample

# Github OAuth Provider
GITHUB_ID=
GITHUB_SECRET=
OPENAI_API_KEY=
GITHUB_ID=sample
GITHUB_SECRET=sample
OPENAI_API_KEY=sample
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: c-hive/gha-yarn-cache@v2
- name: Set Node.js 16.x
- name: Set Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 16.x
node-version: 18.x

- name: Run install
uses: borales/actions-yarn@v4
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM node:18-alpine
WORKDIR /app

COPY . .
COPY /production/.env .env
COPY /production/.env.example .env

RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
Expand Down
7 changes: 7 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
steps:
- name: 'gcr.io/cloud-builders/docker'
args: [
'build', '-t', 'asia-east1-docker.pkg.dev/$PROJECT_ID/twrepo/peerprep-image', '.'
]
images:
- 'asia-east1-docker.pkg.dev/$PROJECT_ID/twrepo/peerprep-image'
1 change: 1 addition & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: '3'
include:
- judge0-v1.13.0/docker-compose.yml
services:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"dotenv-cli": "^7.3.0",
"nanoid": "3.0.0",
"net": "^1.0.2",
"next": "^13.4.13",
"next": "^14.0.1",
"next-auth": "^4.22.4",
"npm-run-all": "^4.1.5",
"openai": "^4.14.1",
Expand Down
46 changes: 12 additions & 34 deletions production/.env.example
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
# Since the ".env" file is gitignored, you can use the ".env.example" file to
# build a new ".env" file when you clone the repo. Keep this file up-to-date
# when you add new variables to `.env`.

## 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}"
# This file will be committed to version control, so make sure not to have any
# secrets in it. If you are cloning this repo, create a copy of this file named
# ".env" and populate it with your secrets.

S3_ENDPOINT="http://localhost:9000"

# When adding additional environment variables, the schema in "/src/env.mjs"
# should be updated accordingly.

# 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=
## Node
NEXT_PUBLIC_WS_PORT=3000
NEXT_PUBLIC_WS_URL=wss://peerprep-j2ljsx7boa-de.a.run.app
NEXT_PUBLIC_PORT=3000
10 changes: 9 additions & 1 deletion src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const env = createEnv({
GITHUB_ID: z.string().min(1),
GITHUB_SECRET: z.string().min(1),
OPENAI_API_KEY: z.string().min(1),
S3_REGION: z.string().optional(),
S3_ACCESS_KEY_ID: z.string(),
S3_SECRET_ACCESS_KEY: z.string()
},

/**
Expand All @@ -36,6 +39,7 @@ export const env = createEnv({
client: {
// NEXT_PUBLIC_CLIENTVAR: z.string().min(1),
NEXT_PUBLIC_WS_PORT: z.string().min(1),
NEXT_PUBLIC_WS_URL: z.string().min(1)
},

/**
Expand All @@ -51,14 +55,18 @@ export const env = createEnv({
MONGO_URL: process.env.MONGO_URL,
NODE_ENV: process.env.NODE_ENV,
NEXTAUTH_SECRET: process.env.NEXT_AUTH_SECRET,
NEXT_PUBLIC_WS_PORT: process.env.NODE_ENV === "production" ? process.env.NEXT_PUBLIC_WS_PORT : "3002",
NEXT_PUBLIC_WS_PORT: process.env.NODE_ENV === "production" ? process.env.NEXT_PUBLIC_PORT : process.env.NEXT_PUBLIC_WS_PORT,
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,
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
NEXT_PUBLIC_WS_URL: process.env.NODE_ENV === "production" ? process.env.NEXT_PUBLIC_WS_URL : `ws://localhost:${process.env.NEXT_PUBLIC_WS_PORT}`,
S3_REGION: process.env.S3_REGION ?? "us-east-1",
S3_ACCESS_KEY_ID: process.env.S3_ACCESS_KEY_ID ?? "S3RVER",
S3_SECRET_ACCESS_KEY: process.env.S3_SECRET_ACCESS_KEY ?? "S3RVER",
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation.
Expand Down
6 changes: 3 additions & 3 deletions src/server/api/routers/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { createTRPCRouter, publicProcedure } from "../trpc";
const UPLOAD_MAX_FILE_SIZE = 400000;

const s3Client = new S3Client({
region: "us-east-1",
region: env.S3_REGION,
endpoint: env.S3_ENDPOINT,
forcePathStyle: true,
credentials: {
accessKeyId: "S3RVER",
secretAccessKey: "S3RVER",
accessKeyId: env.S3_ACCESS_KEY_ID,
secretAccessKey: env.S3_SECRET_ACCESS_KEY,
},
});

Expand Down
40 changes: 20 additions & 20 deletions src/server/prodServer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { appRouter } from "./api/root";
import { applyWSSHandler } from "@trpc/server/adapters/ws";
import http from "http";
import { createServer } from "node:http";
import next from "next";
import { parse } from "url";
import ws from "ws";
import ws, { WebSocketServer } from "ws";
import { createWSTRPCContext } from "./api/trpc";
import { env } from "~/env.mjs";

Expand All @@ -17,26 +17,19 @@ void app
.prepare()
.then(() => {
console.log("Preparing server");
const server = http.createServer((req, res) => {
const proto = req.headers["x-forwarded-proto"];
if (proto && proto === "http") {
// redirect to ssl
res.writeHead(303, {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
location: `https://` + req.headers.host + (req.headers.url ?? ""),
});
res.end();
return;
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const parsedUrl = parse(req.url!, true);
void handle(req, res, parsedUrl);
// eslint-disable-next-line @typescript-eslint/no-misused-promises
const server = createServer(async (req, res) => {
if (!req.url) return;
const parsedUrl = parse(req.url, true);
await handle(req, res, parsedUrl).catch((e) => {
console.log(e);
});
});
console.log("Created http server");
// todo: Can't quite get a http server to pass through to the ws server
/// const wss = new ws.Server({ server });
const wss = new ws.Server({
port: Number(env.NEXT_PUBLIC_WS_PORT) ?? 3002,
const wss = new WebSocketServer({
server,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const handler = applyWSSHandler({
Expand All @@ -45,10 +38,17 @@ void app
createContext: createWSTRPCContext,
});

process.on("SIGTERM", () => {
console.log("SIGTERM");
process.on('SIGTERM', () => {
console.log('SIGTERM');
handler.broadcastReconnectNotification();
});

server.on('upgrade', (req, socket, head) => {
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
});

server.listen(port);

console.log(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function getEndingLink(ctx: NextPageContext | undefined) {
});
}
const client = createWSClient({
url: `ws://localhost:${env.NEXT_PUBLIC_WS_PORT ?? 3002}`,
url: env.NEXT_PUBLIC_WS_URL,
});
return wsLink<AppRouter>({
client,
Expand Down
Loading
Loading