From 5424aa5c9b6c60555143903fbc667a514275cc44 Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Sat, 11 Nov 2023 12:12:01 +0800 Subject: [PATCH 1/6] Cloud deployment --- cloudbuild.yaml | 7 ++ compose.yml | 1 + package.json | 2 +- src/env.mjs | 4 +- src/server/prodServer.ts | 40 +++++----- src/utils/api.ts | 2 +- yarn.lock | 154 +++++++++++++++++++-------------------- 7 files changed, 107 insertions(+), 103 deletions(-) create mode 100644 cloudbuild.yaml diff --git a/cloudbuild.yaml b/cloudbuild.yaml new file mode 100644 index 0000000..261a2ec --- /dev/null +++ b/cloudbuild.yaml @@ -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' diff --git a/compose.yml b/compose.yml index ef9ddaf..83edf6a 100644 --- a/compose.yml +++ b/compose.yml @@ -1,3 +1,4 @@ +version: '3' include: - judge0-v1.13.0/docker-compose.yml services: diff --git a/package.json b/package.json index 0d4aece..f5731b0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/env.mjs b/src/env.mjs index 0debda1..46b5160 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -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) }, /** @@ -51,7 +52,7 @@ 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, @@ -59,6 +60,7 @@ export const env = createEnv({ 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. diff --git a/src/server/prodServer.ts b/src/server/prodServer.ts index df6b867..340063c 100644 --- a/src/server/prodServer.ts +++ b/src/server/prodServer.ts @@ -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"; @@ -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({ @@ -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( diff --git a/src/utils/api.ts b/src/utils/api.ts index 0645bb7..e677699 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -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({ client, diff --git a/yarn.lock b/yarn.lock index 79aaf1f..9b8b5a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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/eslint-plugin-next@13.4.19": version "13.4.19" @@ -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" @@ -1902,10 +1902,10 @@ resolved "https://registry.yarnpkg.com/@stomp/stompjs/-/stompjs-7.0.0.tgz#46b5c454a9dc8262e0b20f3b3dbacaa113993077" integrity sha512-fGdq4wPDnSV/KyOsjq4P+zLc8MFWC3lMmP5FBgLWKPJTYcuCbAIrnRGjB7q2jHZdYCOD5vxLuFoKIYLy5/u8Pw== -"@swc/helpers@0.5.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/helpers@0.5.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" @@ -5478,7 +5478,7 @@ nanoid@3.0.0: 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== @@ -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" @@ -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== -postcss@8.4.14: - version "8.4.14" - resolved "https://registry.npmjs.org/postcss/-/postcss-8.4.14.tgz" - integrity sha512-E398TUmfAYFPBSdzgeieK2Y1+1cpdxJx8yXbK/m57nRhKSmk1GB2tO4lbLBtlkfPQTDKfe4Xqv1ASWPpayPEig== +postcss@8.4.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" @@ -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== -zod@3.21.4: - 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" From 2ad7820e95ac4fb0f7545c7bb5887e24bea4d7b6 Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Sat, 11 Nov 2023 14:41:20 +0800 Subject: [PATCH 2/6] Upgrade node --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2ced3a7..c307cf4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 From cabe44b24692020ea5f88c2f0f7272991c8ed4f1 Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Sat, 11 Nov 2023 14:49:05 +0800 Subject: [PATCH 3/6] Fix env --- .env.example | 2 ++ production/.env.example | 50 ++++++++++------------------------------- 2 files changed, 14 insertions(+), 38 deletions(-) diff --git a/.env.example b/.env.example index 9e6ea45..6b46c33 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,8 @@ 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 diff --git a/production/.env.example b/production/.env.example index f7e8b37..e05094b 100644 --- a/production/.env.example +++ b/production/.env.example @@ -1,41 +1,15 @@ +# 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 -POSTGRES_PASSWORD=password -POSTGRES_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgresdb-dev:5432/db?schema=public" - -## MongoDB -MONGO_USER=mongodb -MONGO_PASSWORD=DNs3kNlSnV9Q8iBO -MONGO_URL="mongodb+srv://${MONGO_USER}:${MONGO_PASSWORD}@cs3219test.heahtzv.mongodb.net/db" - -# MONGO_URL="mongodb://${MONGO_USER}:${MONGO_PASSWORD}@mongodb-dev:27017/db?authSource=admin" -# MONGO_URL="mongodb://mongodb-dev:27017/db" - -# 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=423856fc5cae959fe4c4 -GITHUB_SECRET=96df1e12eeb67a9359213ff14826c538522fdea4 -OPENAI_API_KEY=apple +## Node +NEXT_PUBLIC_WS_PORT=3000 +NEXT_PUBLIC_WS_URL=wss://peerprep-j2ljsx7boa-de.a.run.app +NEXT_PUBLIC_PORT=3000 \ No newline at end of file From 15cd2630b13a3ff842f0b66a73c3096d4b5afe71 Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Sat, 11 Nov 2023 15:57:25 +0800 Subject: [PATCH 4/6] Add s3 env variables --- src/env.mjs | 8 +++++++- src/server/api/routers/form.ts | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/env.mjs b/src/env.mjs index 46b5160..1882a87 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -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() }, /** @@ -60,7 +63,10 @@ export const env = createEnv({ 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}` + 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. diff --git a/src/server/api/routers/form.ts b/src/server/api/routers/form.ts index 608e66a..aa1dd6b 100644 --- a/src/server/api/routers/form.ts +++ b/src/server/api/routers/form.ts @@ -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, }, }); From 0f2de53b5bcc51cd20f1b4b5451ee6af75e68408 Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Sat, 11 Nov 2023 16:36:49 +0800 Subject: [PATCH 5/6] Fix build --- .env.example | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index a412690..956a892 100644 --- a/.env.example +++ b/.env.example @@ -8,12 +8,12 @@ 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 @@ -31,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 From bda8396b4b0be4738b56204309c4044db9efb042 Mon Sep 17 00:00:00 2001 From: "Au Chen Xi, Gabriel" Date: Sat, 11 Nov 2023 17:15:41 +0800 Subject: [PATCH 6/6] Fix dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d3e22cd..116880a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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; \