Skip to content

Commit

Permalink
Cloud deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabau committed Nov 11, 2023
1 parent 3f85a4a commit 5424aa5
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 103 deletions.
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
4 changes: 3 additions & 1 deletion src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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 +52,15 @@ 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}`
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation.
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
154 changes: 74 additions & 80 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1320,10 +1320,10 @@
resolved "https://registry.npmjs.org/@next-auth/prisma-adapter/-/prisma-adapter-1.0.7.tgz"
integrity sha512-Cdko4KfcmKjsyHFrWwZ//lfLUbcLqlyFqjd/nYE2m3aZ7tjMNUjpks47iw7NTCnXf+5UWz5Ypyt1dSs1EP5QJw==

"@next/env@13.4.19":
version "13.4.19"
resolved "https://registry.npmjs.org/@next/env/-/env-13.4.19.tgz"
integrity sha512-FsAT5x0jF2kkhNkKkukhsyYOrRqtSxrEhfliniIq0bwWbuXLgyt3Gv0Ml+b91XwjwArmuP7NxCiGd++GGKdNMQ==
"@next/env@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/env/-/env-14.0.2.tgz#c1fb535983bca768e7eccd7b9cf4636127fc6d25"
integrity sha512-HAW1sljizEaduEOes/m84oUqeIDAUYBR1CDwu2tobNlNDFP3cSm9d6QsOsGeNlIppU1p/p1+bWbYCbvwjFiceA==

"@next/[email protected]":
version "13.4.19"
Expand All @@ -1332,50 +1332,50 @@
dependencies:
glob "7.1.7"

"@next/swc-darwin-arm64@13.4.19":
version "13.4.19"
resolved "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.19.tgz"
integrity sha512-vv1qrjXeGbuF2mOkhkdxMDtv9np7W4mcBtaDnHU+yJG+bBwa6rYsYSCI/9Xm5+TuF5SbZbrWO6G1NfTh1TMjvQ==

"@next/swc-darwin-x64@13.4.19":
version "13.4.19"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.19.tgz#aebe38713a4ce536ee5f2a291673e14b715e633a"
integrity sha512-jyzO6wwYhx6F+7gD8ddZfuqO4TtpJdw3wyOduR4fxTUCm3aLw7YmHGYNjS0xRSYGAkLpBkH1E0RcelyId6lNsw==

"@next/swc-linux-arm64-gnu@13.4.19":
version "13.4.19"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.19.tgz#ec54db65b587939c7b94f9a84800f003a380f5a6"
integrity sha512-vdlnIlaAEh6H+G6HrKZB9c2zJKnpPVKnA6LBwjwT2BTjxI7e0Hx30+FoWCgi50e+YO49p6oPOtesP9mXDRiiUg==

"@next/swc-linux-arm64-musl@13.4.19":
version "13.4.19"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.19.tgz#1f5e2c1ea6941e7d530d9f185d5d64be04279d86"
integrity sha512-aU0HkH2XPgxqrbNRBFb3si9Ahu/CpaR5RPmN2s9GiM9qJCiBBlZtRTiEca+DC+xRPyCThTtWYgxjWHgU7ZkyvA==

"@next/swc-linux-x64-gnu@13.4.19":
version "13.4.19"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.19.tgz#96b0882492a2f7ffcce747846d3680730f69f4d1"
integrity sha512-htwOEagMa/CXNykFFeAHHvMJeqZfNQEoQvHfsA4wgg5QqGNqD5soeCer4oGlCol6NGUxknrQO6VEustcv+Md+g==

"@next/swc-linux-x64-musl@13.4.19":
version "13.4.19"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.19.tgz#f276b618afa321d2f7b17c81fc83f429fb0fd9d8"
integrity sha512-4Gj4vvtbK1JH8ApWTT214b3GwUh9EKKQjY41hH/t+u55Knxi/0wesMzwQRhppK6Ddalhu0TEttbiJ+wRcoEj5Q==

"@next/swc-win32-arm64-msvc@13.4.19":
version "13.4.19"
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.19.tgz#1599ae0d401da5ffca0947823dac577697cce577"
integrity sha512-bUfDevQK4NsIAHXs3/JNgnvEY+LRyneDN788W2NYiRIIzmILjba7LaQTfihuFawZDhRtkYCv3JDC3B4TwnmRJw==

"@next/swc-win32-ia32-msvc@13.4.19":
version "13.4.19"
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.19.tgz#55cdd7da90818f03e4da16d976f0cb22045d16fd"
integrity sha512-Y5kikILFAr81LYIFaw6j/NrOtmiM4Sf3GtOc0pn50ez2GCkr+oejYuKGcwAwq3jiTKuzF6OF4iT2INPoxRycEA==

"@next/swc-win32-x64-msvc@13.4.19":
version "13.4.19"
resolved "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.19.tgz"
integrity sha512-YzA78jBDXMYiINdPdJJwGgPNT3YqBNNGhsthsDoWHL9p24tEJn9ViQf/ZqTbwSpX/RrkPupLfuuTH2sf73JBAw==
"@next/swc-darwin-arm64@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.0.2.tgz#eba35a1425fee5d305903c85ae0d6d2b0d512c7b"
integrity sha512-i+jQY0fOb8L5gvGvojWyZMfQoQtDVB2kYe7fufOEiST6sicvzI2W5/EXo4lX5bLUjapHKe+nFxuVv7BA+Pd7LQ==

"@next/swc-darwin-x64@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-14.0.2.tgz#8adb4dfc3d596c0816da67df9b75603218cf2a42"
integrity sha512-zRCAO0d2hW6gBEa4wJaLn+gY8qtIqD3gYd9NjruuN98OCI6YyelmhWVVLlREjS7RYrm9OUQIp/iVJFeB6kP1hg==

"@next/swc-linux-arm64-gnu@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.0.2.tgz#1f88d066d44c9229a861815e3d449b0037dae14e"
integrity sha512-tSJmiaon8YaKsVhi7GgRizZoV0N1Sx5+i+hFTrCKKQN7s3tuqW0Rov+RYdPhAv/pJl4qiG+XfSX4eJXqpNg3dA==

"@next/swc-linux-arm64-musl@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.0.2.tgz#de9b2708abc35dd19429a662a11785d0c54d1ec7"
integrity sha512-dXJLMSEOwqJKcag1BeX1C+ekdPPJ9yXbWIt3nAadhbLx5CjACoB2NQj9Xcqu2tmdr5L6m34fR+fjGPs+ZVPLzA==

"@next/swc-linux-x64-gnu@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.0.2.tgz#64bd555dcbc7fd6c38cb86028baf7d7fc80bd4ac"
integrity sha512-WC9KAPSowj6as76P3vf1J3mf2QTm3Wv3FBzQi7UJ+dxWjK3MhHVWsWUo24AnmHx9qDcEtHM58okgZkXVqeLB+Q==

"@next/swc-linux-x64-musl@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.0.2.tgz#69e6abf0f516df69acbf663eeb8ed6fd8eebcc38"
integrity sha512-KSSAwvUcjtdZY4zJFa2f5VNJIwuEVnOSlqYqbQIawREJA+gUI6egeiRu290pXioQXnQHYYdXmnVNZ4M+VMB7KQ==

"@next/swc-win32-arm64-msvc@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.0.2.tgz#82bc49af0986f4b2c113b5f223a559fc51b49b9d"
integrity sha512-2/O0F1SqJ0bD3zqNuYge0ok7OEWCQwk55RPheDYD0va5ij7kYwrFkq5ycCRN0TLjLfxSF6xI5NM6nC5ux7svEQ==

"@next/swc-win32-ia32-msvc@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.0.2.tgz#2f1958ad82b7f7ec5da8ad8ac2f18ef7a8e7757f"
integrity sha512-vJI/x70Id0oN4Bq/R6byBqV1/NS5Dl31zC+lowO8SDu1fHmUxoAdILZR5X/sKbiJpuvKcCrwbYgJU8FF/Gh50Q==

"@next/swc-win32-x64-msvc@14.0.2":
version "14.0.2"
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.0.2.tgz#629174f587beb640a431a4a3fe4e26d5d4f8de52"
integrity sha512-Ut4LXIUvC5m8pHTe2j0vq/YDnTEyq6RSR9vHYPqnELrDapPhLNz9Od/L5Ow3J8RNDWpEnfCiQXuVdfjlNEJ7ug==

"@nextjournal/lang-clojure@^1.0.0":
version "1.0.0"
Expand Down Expand Up @@ -1902,10 +1902,10 @@
resolved "https://registry.yarnpkg.com/@stomp/stompjs/-/stompjs-7.0.0.tgz#46b5c454a9dc8262e0b20f3b3dbacaa113993077"
integrity sha512-fGdq4wPDnSV/KyOsjq4P+zLc8MFWC3lMmP5FBgLWKPJTYcuCbAIrnRGjB7q2jHZdYCOD5vxLuFoKIYLy5/u8Pw==

"@swc/[email protected].1":
version "0.5.1"
resolved "https://registry.npmjs.org/@swc/helpers/-/helpers-0.5.1.tgz"
integrity sha512-sJ902EfIzn1Fa+qYmjdQqh8tPsoxyBz+8yBKC2HKUxyezKJFwPGOn7pv4WY6QuQW//ySQi5lJjA/ZT9sNWWNTg==
"@swc/[email protected].2":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.5.2.tgz#85ea0c76450b61ad7d10a37050289eded783c27d"
integrity sha512-E4KcWTpoLHqwPHLxidpOqQbcrZVgi0rsmmZXUle1jXmJfuIf/UWpczUJ7MZZ5tlxytgJXyp0w4PGkkeLiuIdZw==
dependencies:
tslib "^2.4.0"

Expand Down Expand Up @@ -5478,7 +5478,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.0.0.tgz#760f6d7e076935d086aa3c790e0272cab41abbcb"
integrity sha512-5lU+F2QpTW1JLwlXxnu2/jkpTipIuYZL3Za6TQzM16HYDzZQqlthbXYSW0H2KzZu1UQPGHDPbCl1TS3xOKfqEQ==

nanoid@^3.3.4, nanoid@^3.3.6:
nanoid@^3.3.6:
version "3.3.6"
resolved "https://registry.npmjs.org/nanoid/-/nanoid-3.3.6.tgz"
integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==
Expand Down Expand Up @@ -5518,29 +5518,28 @@ next-auth@^4.22.4:
preact-render-to-string "^5.1.19"
uuid "^8.3.2"

next@^13.4.13:
version "13.4.19"
resolved "https://registry.npmjs.org/next/-/next-13.4.19.tgz"
integrity sha512-HuPSzzAbJ1T4BD8e0bs6B9C1kWQ6gv8ykZoRWs5AQoiIuqbGHHdQO7Ljuvg05Q0Z24E2ABozHe6FxDvI6HfyAw==
next@^14.0.1:
version "14.0.2"
resolved "https://registry.yarnpkg.com/next/-/next-14.0.2.tgz#02ba6a1656edf14d3913c7a3553026e9d6e083c7"
integrity sha512-jsAU2CkYS40GaQYOiLl9m93RTv2DA/tTJ0NRlmZIBIL87YwQ/xR8k796z7IqgM3jydI8G25dXvyYMC9VDIevIg==
dependencies:
"@next/env" "13.4.19"
"@swc/helpers" "0.5.1"
"@next/env" "14.0.2"
"@swc/helpers" "0.5.2"
busboy "1.6.0"
caniuse-lite "^1.0.30001406"
postcss "8.4.14"
postcss "8.4.31"
styled-jsx "5.1.1"
watchpack "2.4.0"
zod "3.21.4"
optionalDependencies:
"@next/swc-darwin-arm64" "13.4.19"
"@next/swc-darwin-x64" "13.4.19"
"@next/swc-linux-arm64-gnu" "13.4.19"
"@next/swc-linux-arm64-musl" "13.4.19"
"@next/swc-linux-x64-gnu" "13.4.19"
"@next/swc-linux-x64-musl" "13.4.19"
"@next/swc-win32-arm64-msvc" "13.4.19"
"@next/swc-win32-ia32-msvc" "13.4.19"
"@next/swc-win32-x64-msvc" "13.4.19"
"@next/swc-darwin-arm64" "14.0.2"
"@next/swc-darwin-x64" "14.0.2"
"@next/swc-linux-arm64-gnu" "14.0.2"
"@next/swc-linux-arm64-musl" "14.0.2"
"@next/swc-linux-x64-gnu" "14.0.2"
"@next/swc-linux-x64-musl" "14.0.2"
"@next/swc-win32-arm64-msvc" "14.0.2"
"@next/swc-win32-ia32-msvc" "14.0.2"
"@next/swc-win32-x64-msvc" "14.0.2"

nice-try@^1.0.4:
version "1.0.5"
Expand Down Expand Up @@ -5962,12 +5961,12 @@ postcss-value-parser@^4.0.0, postcss-value-parser@^4.2.0:
resolved "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz"
integrity sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==

[email protected].14:
version "8.4.14"
resolved "https://registry.npmjs.org/postcss/-/postcss-8.4.14.tgz"
integrity sha512-E398TUmfAYFPBSdzgeieK2Y1+1cpdxJx8yXbK/m57nRhKSmk1GB2tO4lbLBtlkfPQTDKfe4Xqv1ASWPpayPEig==
[email protected].31:
version "8.4.31"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.31.tgz#92b451050a9f914da6755af352bdc0192508656d"
integrity sha512-PS08Iboia9mts/2ygV3eLpY5ghnUcfLV/EXTOW1E2qYxJKGGBUtNjN76FYHnMs36RmARn41bC0AZmn+rR0OVpQ==
dependencies:
nanoid "^3.3.4"
nanoid "^3.3.6"
picocolors "^1.0.0"
source-map-js "^1.0.2"

Expand Down Expand Up @@ -7508,11 +7507,6 @@ yocto-queue@^0.1.0:
resolved "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

[email protected]:
version "3.21.4"
resolved "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz"
integrity sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==

zod@^3.21.4:
version "3.22.2"
resolved "https://registry.npmjs.org/zod/-/zod-3.22.2.tgz"
Expand Down

0 comments on commit 5424aa5

Please sign in to comment.