From f76b2fc60f1483b353d78ef7df6fda04639fdb19 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Thu, 9 Jan 2025 19:19:34 +0200 Subject: [PATCH 01/13] fix: Use environment variable to determine https redirection --- view/_partial/header.ejs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/view/_partial/header.ejs b/view/_partial/header.ejs index 55d5c026..e5d8c789 100644 --- a/view/_partial/header.ejs +++ b/view/_partial/header.ejs @@ -38,7 +38,17 @@ <% // https://stackoverflow.com/a/4723302 %> <% // https://stackoverflow.com/a/6222280 %> - + <% if (HTTPS_ENABLED) { %> + + <% } else { %> + + <% } %>
From 0103bb61f876e4fe360fa26f4b8ad75bb2223269 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Thu, 9 Jan 2025 19:21:39 +0200 Subject: [PATCH 02/13] fix: Pass HTTPS_ENABLED in routing --- src/lostcity/routes/website.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lostcity/routes/website.js b/src/lostcity/routes/website.js index 9e10e3f2..434472b3 100644 --- a/src/lostcity/routes/website.js +++ b/src/lostcity/routes/website.js @@ -6,7 +6,10 @@ import Environment from '#lostcity/util/Environment.js'; export default function (f, opts, next) { f.get('/', async (req, res) => { - return res.view('index'); + // Convert env variable to boolean + const HTTPS_ENABLED = process.env.HTTPS_ENABLED === 'true'; + // Pass for header.ejs to determine https redirection + return res.view('index', {HTTPS_ENABLED: HTTPS_ENABLED}); }); f.get('/disclaimer', async (req, res) => { From ed8d90a755f282d477d296364f6101b8202dcab7 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Thu, 9 Jan 2025 20:06:58 +0200 Subject: [PATCH 03/13] fix: Use existing import to pass https var --- src/lostcity/routes/mod/index.ts | 2 ++ src/lostcity/routes/news/index.ts | 2 ++ src/lostcity/routes/player/index.ts | 2 ++ src/lostcity/routes/website.js | 15 +++++++-------- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/lostcity/routes/mod/index.ts b/src/lostcity/routes/mod/index.ts index 4752945d..39fefc9d 100644 --- a/src/lostcity/routes/mod/index.ts +++ b/src/lostcity/routes/mod/index.ts @@ -1,4 +1,5 @@ import { db } from '#lostcity/db/query.js'; +import Environment from '#lostcity/util/Environment.js'; export default function (f: any, opts: any, next: any) { f.get('/session/:username', async (req: any, res: any) => { @@ -8,6 +9,7 @@ export default function (f: any, opts: any, next: any) { const account = await db.selectFrom('account').where('username', '=', username).selectAll().executeTakeFirstOrThrow(); return res.view('mod/session', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, account, logs: await db.selectFrom('account_session').where('account_id', '=', account.id).orderBy('timestamp desc').limit(100).selectAll().execute() }); diff --git a/src/lostcity/routes/news/index.ts b/src/lostcity/routes/news/index.ts index b0a05b33..eee3216e 100644 --- a/src/lostcity/routes/news/index.ts +++ b/src/lostcity/routes/news/index.ts @@ -58,6 +58,7 @@ export default function (f: any, opts: any, next: any) { newsposts = newsposts.limit(17); return res.view('news/index', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, category, page, more, @@ -78,6 +79,7 @@ export default function (f: any, opts: any, next: any) { const next = await db.selectFrom('newspost').where('id', '>', req.params.id).where('category_id', '=', newspost.category_id).orderBy('id', 'asc').select('id').executeTakeFirst(); return res.view('news/post', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, newspost, category, date: niceDate(newspost.date), diff --git a/src/lostcity/routes/player/index.ts b/src/lostcity/routes/player/index.ts index 90904256..3b6fb86c 100644 --- a/src/lostcity/routes/player/index.ts +++ b/src/lostcity/routes/player/index.ts @@ -1,4 +1,5 @@ import { db } from '#lostcity/db/query.js'; +import Environment from '#lostcity/util/Environment.js'; import LoggerEventType from '#lostcity/util/LoggerEventType.js'; export default function (f: any, opts: any, next: any) { @@ -9,6 +10,7 @@ export default function (f: any, opts: any, next: any) { const account = await db.selectFrom('account').where('username', '=', username).selectAll().executeTakeFirstOrThrow(); return res.view('player/adventurelog', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, account, logs: await db.selectFrom('account_session').where('account_id', '=', account.id).where('event_type', '=', LoggerEventType.ADVENTURE).orderBy('timestamp desc').limit(100).selectAll().execute() }); diff --git a/src/lostcity/routes/website.js b/src/lostcity/routes/website.js index 434472b3..b8100c81 100644 --- a/src/lostcity/routes/website.js +++ b/src/lostcity/routes/website.js @@ -6,14 +6,11 @@ import Environment from '#lostcity/util/Environment.js'; export default function (f, opts, next) { f.get('/', async (req, res) => { - // Convert env variable to boolean - const HTTPS_ENABLED = process.env.HTTPS_ENABLED === 'true'; - // Pass for header.ejs to determine https redirection - return res.view('index', {HTTPS_ENABLED: HTTPS_ENABLED}); + return res.view('index', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/disclaimer', async (req, res) => { - return res.view('disclaimer'); + return res.view('disclaimer', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/title', async (req, res) => { @@ -24,6 +21,7 @@ export default function (f, opts, next) { const latestNews = Environment.DB_HOST ? await db.selectFrom('newspost').orderBy('id', 'desc').limit(5).selectAll().execute() : []; return res.view('title', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, playerCount, newsposts: latestNews }); @@ -62,6 +60,7 @@ export default function (f, opts, next) { } return res.view('serverlist', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, detail: typeof req.query['hires.x'] !== 'undefined' ? 'high' : 'low', method: req.query.method, worlds: WorldList, @@ -93,7 +92,7 @@ export default function (f, opts, next) { }); f.get('/privacy', async (req, res) => { - return res.view('privacy'); + return res.view('privacy', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/support', async (req, res) => { @@ -101,11 +100,11 @@ export default function (f, opts, next) { }); f.get('/terms', async (req, res) => { - return res.view('terms'); + return res.view('terms', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/whychoosers', async (req, res) => { - return res.view('whychoosers'); + return res.view('whychoosers', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/worldmap', async (req, res) => { From bb49899a287211b27a3c13a69c2eda34e97d678d Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Thu, 9 Jan 2025 20:09:34 +0200 Subject: [PATCH 04/13] fix: Pass env to news routes --- src/lostcity/routes/news/index.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lostcity/routes/news/index.ts b/src/lostcity/routes/news/index.ts index eee3216e..3ca8495d 100644 --- a/src/lostcity/routes/news/index.ts +++ b/src/lostcity/routes/news/index.ts @@ -107,6 +107,7 @@ export default function (f: any, opts: any, next: any) { const newspost = await db.selectFrom('newspost').where('id', '=', post).selectAll().executeTakeFirst(); if (newspost) { return res.view('news/create', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, categories, date: niceDate(newspost.date), post, @@ -116,6 +117,7 @@ export default function (f: any, opts: any, next: any) { } return res.view('news/create', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, categories }); }); @@ -162,6 +164,7 @@ export default function (f: any, opts: any, next: any) { const categories = await db.selectFrom('newspost_category').selectAll().execute(); return res.view('news/create', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, categories, post, date: niceDate(new Date()), @@ -185,6 +188,7 @@ export default function (f: any, opts: any, next: any) { const newspost = await db.selectFrom('newspost').where('id', '=', post).selectAll().executeTakeFirst(); if (newspost) { return res.view('news/create', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, categories, post, newspost, @@ -197,6 +201,7 @@ export default function (f: any, opts: any, next: any) { } return res.view('news/create', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, categories, post, date: niceDate(new Date()), From a41d018cd6db7943defdaf60f61d5ea9fa7963b2 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Thu, 9 Jan 2025 20:10:59 +0200 Subject: [PATCH 05/13] fix: Pass env to detail, username --- src/lostcity/routes/create/index.ts | 3 +++ src/lostcity/routes/website.js | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lostcity/routes/create/index.ts b/src/lostcity/routes/create/index.ts index 04d7f69e..6e6d6a60 100644 --- a/src/lostcity/routes/create/index.ts +++ b/src/lostcity/routes/create/index.ts @@ -4,6 +4,8 @@ import { toDisplayName, toSafeName } from '#jagex2/jstring/JString.js'; import { db } from '#lostcity/db/query.js'; +import Environment from '#lostcity/util/Environment.js'; + enum CreateStep { USERNAME, TERMS, @@ -37,6 +39,7 @@ export default function (f: any, opts: any, next: any) { delete req.session.createUsername; return res.view('create/username', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, error: createError }); } else if (createStep === CreateStep.TERMS) { diff --git a/src/lostcity/routes/website.js b/src/lostcity/routes/website.js index b8100c81..b54ea923 100644 --- a/src/lostcity/routes/website.js +++ b/src/lostcity/routes/website.js @@ -84,7 +84,7 @@ export default function (f, opts, next) { }); f.get('/detail', async (req, res) => { - return res.view('detail'); + return res.view('detail', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/manual', async (req, res) => { @@ -112,7 +112,7 @@ export default function (f, opts, next) { }); f.get('/downloads', async (req, res) => { - return res.view('downloads'); + return res.view('downloads', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); next(); From 9995cb5d258d288210c1f7a96ddea6cb2681d2f7 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Thu, 9 Jan 2025 20:11:55 +0200 Subject: [PATCH 06/13] fix: Pass env var to creation routes --- src/lostcity/routes/create/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lostcity/routes/create/index.ts b/src/lostcity/routes/create/index.ts index 6e6d6a60..48420346 100644 --- a/src/lostcity/routes/create/index.ts +++ b/src/lostcity/routes/create/index.ts @@ -44,10 +44,12 @@ export default function (f: any, opts: any, next: any) { }); } else if (createStep === CreateStep.TERMS) { return res.view('create/terms', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, username: createUsername }); } else if (createStep === CreateStep.PASSWORD) { return res.view('create/password', { + HTTPS_ENABLED: Environment.HTTPS_ENABLED, username: createUsername, error: createError }); @@ -55,7 +57,7 @@ export default function (f: any, opts: any, next: any) { delete req.session.createStep; delete req.session.createUsername; - return res.view('create/finish'); + return res.view('create/finish', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); } }); From f0d857e2275cd76d8ea02779f22eeba90e7407c2 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Thu, 9 Jan 2025 20:13:07 +0200 Subject: [PATCH 07/13] fix: Pass env var to about --- src/lostcity/routes/about/index.js | 10 ++++++---- src/lostcity/routes/website.js | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lostcity/routes/about/index.js b/src/lostcity/routes/about/index.js index 5d7df6fc..3b0156ec 100644 --- a/src/lostcity/routes/about/index.js +++ b/src/lostcity/routes/about/index.js @@ -1,18 +1,20 @@ +import Environment from '#lostcity/util/Environment.js'; + export default function (f, opts, next) { f.get('/', async (req, res) => { - return res.view('about/index'); + return res.view('about/index', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/getstart', async (req, res) => { - return res.view('about/getstart'); + return res.view('about/getstart', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/virtual', async (req, res) => { - return res.view('about/virtual'); + return res.view('about/virtual', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/whatisrs', async (req, res) => { - return res.view('about/whatisrs'); + return res.view('about/whatisrs', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); next(); diff --git a/src/lostcity/routes/website.js b/src/lostcity/routes/website.js index b54ea923..aa3b0adb 100644 --- a/src/lostcity/routes/website.js +++ b/src/lostcity/routes/website.js @@ -76,7 +76,7 @@ export default function (f, opts, next) { }); f.get('/cookies', async (req, res) => { - return res.view('cookies'); + return res.view('cookies', {HTTPS_ENABLED: Environment.HTTPS_ENABLED}); }); f.get('/copyright', async (req, res) => { From 09904871307a39d8fcefda2a82443592b40d4fb1 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Fri, 10 Jan 2025 15:53:23 +0200 Subject: [PATCH 08/13] fix: Remove redundant else block --- view/_partial/header.ejs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/view/_partial/header.ejs b/view/_partial/header.ejs index e5d8c789..5bfacb8a 100644 --- a/view/_partial/header.ejs +++ b/view/_partial/header.ejs @@ -44,10 +44,6 @@ src="https://2004scape.org/img/blank.gif" onload="if (location.protocol !== 'https:') { location.replace(`https:${location.href.substring(location.protocol.length)}`); }" > - <% } else { %> - <% } %> From a5c1cbef9fd43d253e2505079742bf2184fd5ef7 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Fri, 10 Jan 2025 16:17:25 +0200 Subject: [PATCH 09/13] feat(infra): Attempt to run a javascript world configuration via composer --- src/lostcity/util/dockerWorldsConf.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/lostcity/util/dockerWorldsConf.js diff --git a/src/lostcity/util/dockerWorldsConf.js b/src/lostcity/util/dockerWorldsConf.js new file mode 100644 index 00000000..0bdddb17 --- /dev/null +++ b/src/lostcity/util/dockerWorldsConf.js @@ -0,0 +1,24 @@ +import fs from 'fs'; +import path from 'path'; + +const worldConfig = [ + { + id: 10, + region: process.env.REGION || 'Docker build test', + address: `http://localhost:${process.env.WEB_PORT || 3000}`, + members: process.env.MEMBERS_WORLD === 'true', + portOffset: 0 + } +]; + +const filePath = path.resolve('/usr/src/app/data/config/worlds.json'); + +fs.mkdirSync(path.dirname(filePath), { recursive: true }); +fs.writeFileSync(filePath, JSON.stringify(worldConfig, null, 2)); + +console.log('[INFO] Worlds config generated! Much wow '); +console.log(` + (\\(\\ + ( -.-) ☆彡 + o_(")(") You're a star!` +); From dc4abe2257d894681af57ce71ccdbb0322729239 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Fri, 10 Jan 2025 16:37:20 +0200 Subject: [PATCH 10/13] feat(infra): Attempt to pass worlds from server repository to composer, to website container --- src/lostcity/util/dockerWorldsConf.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/lostcity/util/dockerWorldsConf.js b/src/lostcity/util/dockerWorldsConf.js index 0bdddb17..78e03465 100644 --- a/src/lostcity/util/dockerWorldsConf.js +++ b/src/lostcity/util/dockerWorldsConf.js @@ -1,24 +1,15 @@ import fs from 'fs'; import path from 'path'; - -const worldConfig = [ - { - id: 10, - region: process.env.REGION || 'Docker build test', - address: `http://localhost:${process.env.WEB_PORT || 3000}`, - members: process.env.MEMBERS_WORLD === 'true', - portOffset: 0 - } -]; +import worldConfig from '/usr/src/app/prodWorldsConfig.js'; const filePath = path.resolve('/usr/src/app/data/config/worlds.json'); -fs.mkdirSync(path.dirname(filePath), { recursive: true }); +fs.mkdirSync(path.dirname(filePath), {recursive: true}); fs.writeFileSync(filePath, JSON.stringify(worldConfig, null, 2)); console.log('[INFO] Worlds config generated! Much wow '); console.log(` (\\(\\ ( -.-) ☆彡 - o_(")(") You're a star!` + o_(")(") You're a star!\n` ); From 14da843f51888d6679a485c64a560a6b3bdc2bb8 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Fri, 10 Jan 2025 16:47:11 +0200 Subject: [PATCH 11/13] feat(infra): Attempt to fix path conflict with composer --- src/lostcity/util/dockerWorldsConf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lostcity/util/dockerWorldsConf.js b/src/lostcity/util/dockerWorldsConf.js index 78e03465..281345cb 100644 --- a/src/lostcity/util/dockerWorldsConf.js +++ b/src/lostcity/util/dockerWorldsConf.js @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import worldConfig from '/usr/src/app/prodWorldsConfig.js'; +import worldConfig from '/usr/src/prodWorldsConfig.js'; const filePath = path.resolve('/usr/src/app/data/config/worlds.json'); From 794cac429b3e8ecf12f83cb4197a6341ef2a84ac Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Fri, 10 Jan 2025 16:50:10 +0200 Subject: [PATCH 12/13] feat(infra): Fix file name from server repo --- src/lostcity/util/dockerWorldsConf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lostcity/util/dockerWorldsConf.js b/src/lostcity/util/dockerWorldsConf.js index 281345cb..562e6bb7 100644 --- a/src/lostcity/util/dockerWorldsConf.js +++ b/src/lostcity/util/dockerWorldsConf.js @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import worldConfig from '/usr/src/prodWorldsConfig.js'; +import worldConfig from '/usr/src/prodWorldsConf.js'; const filePath = path.resolve('/usr/src/app/data/config/worlds.json'); From 22994c32476d99ba18ba3f64a351129e4a64e176 Mon Sep 17 00:00:00 2001 From: Rohanlogs Date: Fri, 10 Jan 2025 17:01:41 +0200 Subject: [PATCH 13/13] feat(infra): Add doc string for the javascript --- src/lostcity/util/dockerWorldsConf.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lostcity/util/dockerWorldsConf.js b/src/lostcity/util/dockerWorldsConf.js index 562e6bb7..ed64d4be 100644 --- a/src/lostcity/util/dockerWorldsConf.js +++ b/src/lostcity/util/dockerWorldsConf.js @@ -1,3 +1,17 @@ +/** + * @fileoverview + * This script dynamically loads a world configuration file from the Server repo. + * + * Purpose: + * - This script loads worlds file dynamically from server repo + * - Its meant to be used only with docker production build + * + * Why? + * - You don't have to pull and modify Website repository just to configure your worlds + * - Simplifies running prod setup + * - Keeps the Server repo as the single source +*/ + import fs from 'fs'; import path from 'path'; import worldConfig from '/usr/src/prodWorldsConf.js';