From d9e49f3438f5f4350141b745224710eb79757623 Mon Sep 17 00:00:00 2001 From: oleksandr-shysh Date: Mon, 16 Jun 2025 12:16:03 +0300 Subject: [PATCH] Made small refactoring --- src/client/HostLobbyModal.ts | 15 +++++++++++++-- src/client/JoinPrivateLobbyModal.ts | 4 +--- src/client/PublicLobby.ts | 2 +- src/client/jwt.ts | 10 +++++----- src/core/configuration/ConfigLoader.ts | 2 +- src/core/configuration/DefaultConfig.ts | 2 +- src/server/cors.ts | 18 ++++++++++-------- webpack.config.js | 2 ++ 8 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/client/HostLobbyModal.ts b/src/client/HostLobbyModal.ts index 6053e5a6c..b079925ee 100644 --- a/src/client/HostLobbyModal.ts +++ b/src/client/HostLobbyModal.ts @@ -585,13 +585,24 @@ async function createLobby(): Promise { } } +// Cache for storing computed game URLs +const urlCache = new Map(); + export async function buildGameUrl( gameID: string, path: string, ): Promise { + const cacheKey = `${gameID}:${path}`; + if (urlCache.has(cacheKey)) { + return urlCache.get(cacheKey)!; + } + const config = await getServerConfigFromClient(); - const apiPath = `/api/${path}/${gameID}`; + const apiPath = `/api/${path === "exists" ? "game" : path}/${gameID}${path === "exists" ? "/exists" : ""}`; const baseUrl = process.env.APP_BASE_URL || ""; - return `${baseUrl}/${config.workerPath(gameID)}${apiPath}`; + const url = `${baseUrl}/${config.workerPath(gameID)}${apiPath}`; + + urlCache.set(cacheKey, url); + return url; } diff --git a/src/client/JoinPrivateLobbyModal.ts b/src/client/JoinPrivateLobbyModal.ts index b9b2fa93b..3b50238a5 100644 --- a/src/client/JoinPrivateLobbyModal.ts +++ b/src/client/JoinPrivateLobbyModal.ts @@ -1,7 +1,6 @@ import { LitElement, html } from "lit"; import { customElement, query, state } from "lit/decorators.js"; import { translateText } from "../client/Utils"; -import { getServerConfigFromClient } from "../core/configuration/ConfigLoader"; import { GameInfo, GameRecord } from "../core/Schemas"; import { generateID } from "../core/Util"; import "./components/baseComponents/Button"; @@ -172,8 +171,7 @@ export class JoinPrivateLobbyModal extends LitElement { } private async checkActiveLobby(lobbyId: string): Promise { - const config = await getServerConfigFromClient(); - const url = `${process.env.APP_BASE_URL || ""}/${config.workerPath(lobbyId)}/api/game/${lobbyId}/exists`; + const url = await buildGameUrl(lobbyId, "exists"); const response = await fetch(url, { method: "GET", diff --git a/src/client/PublicLobby.ts b/src/client/PublicLobby.ts index 69fae6cb4..7f981d2a2 100644 --- a/src/client/PublicLobby.ts +++ b/src/client/PublicLobby.ts @@ -57,7 +57,7 @@ export class PublicLobby extends LitElement { async fetchLobbies(): Promise { try { const response = await fetch( - `${process.env.APP_BASE_URL}/api/public_lobbies`, + `${process.env.APP_BASE_URL || ""}/api/public_lobbies`, ); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); diff --git a/src/client/jwt.ts b/src/client/jwt.ts index b9403c6ec..36e013c69 100644 --- a/src/client/jwt.ts +++ b/src/client/jwt.ts @@ -28,7 +28,9 @@ class BrowserPlatform implements Platform { } getApiBaseForLocalhost(): string { - return localStorage.getItem("apiHost") ?? "http://localhost:8787"; + return ( + localStorage.getItem("apiHost") ?? (process.env.LOCAL_API_BASE_URL || "") + ); } initializeAuthListener(): void { @@ -38,7 +40,7 @@ class BrowserPlatform implements Platform { class CapacitorPlatform implements Platform { getRedirectUri(): string { - return `${process.env.APP_BASE_URL}/discord-redirect.html`; + return `${process.env.APP_BASE_URL || ""}/discord-redirect.html`; } async setLocation(url: string): Promise { @@ -46,9 +48,7 @@ class CapacitorPlatform implements Platform { } getApiBaseForLocalhost(): string { - return process.env.APP_BASE_URL - ? process.env.APP_BASE_URL!.replace("9000", "8787") - : "http://localhost:8787"; + return process.env.LOCAL_API_BASE_URL || ""; } initializeAuthListener(): void { diff --git a/src/core/configuration/ConfigLoader.ts b/src/core/configuration/ConfigLoader.ts index d10d81568..989bff484 100644 --- a/src/core/configuration/ConfigLoader.ts +++ b/src/core/configuration/ConfigLoader.ts @@ -29,7 +29,7 @@ export async function getServerConfigFromClient(): Promise { if (cachedSC) { return cachedSC; } - const response = await fetch(`${process.env.APP_BASE_URL}/api/env`); + const response = await fetch(`${process.env.APP_BASE_URL || ""}/api/env`); if (!response.ok) { throw new Error( diff --git a/src/core/configuration/DefaultConfig.ts b/src/core/configuration/DefaultConfig.ts index aeb4356f5..b52d855de 100644 --- a/src/core/configuration/DefaultConfig.ts +++ b/src/core/configuration/DefaultConfig.ts @@ -102,7 +102,7 @@ export abstract class DefaultServerConfig implements ServerConfig { jwtIssuer(): string { const audience = this.jwtAudience(); return audience === "localhost" - ? "http://localhost:8787" + ? process.env.LOCAL_API_BASE_URL || "" : `https://api.${audience}`; } async jwkPublicKey(): Promise { diff --git a/src/server/cors.ts b/src/server/cors.ts index e22c0935e..9e97b5936 100644 --- a/src/server/cors.ts +++ b/src/server/cors.ts @@ -5,25 +5,27 @@ import { getServerConfigFromServer } from "../core/configuration/ConfigLoader"; const config = getServerConfigFromServer(); const origin = config.origin(); -const allowedOrigins: string[] = [origin]; +const allowedOriginsSet = new Set([origin]); switch (config.env()) { case GameEnv.Prod: - allowedOrigins.push("capacitor://openfront.io", "https://openfront.io"); + allowedOriginsSet.add("capacitor://openfront.io"); + allowedOriginsSet.add("https://openfront.io"); break; case GameEnv.Preprod: - allowedOrigins.push("capacitor://openfront.dev", "https://openfront.dev"); + allowedOriginsSet.add("capacitor://openfront.dev"); + allowedOriginsSet.add("https://openfront.dev"); break; case GameEnv.Dev: { - allowedOrigins.push( - "capacitor://localhost", - "http://localhost", - "http://localhost:8787", - ); + allowedOriginsSet.add("capacitor://localhost"); + allowedOriginsSet.add("http://localhost"); + allowedOriginsSet.add("http://localhost:8787"); break; } } +const allowedOrigins = Array.from(allowedOriginsSet); + const corsOptions = { origin: ( origin: string | undefined, diff --git a/webpack.config.js b/webpack.config.js index ae5eb29fd..71299495c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -35,6 +35,7 @@ export default async (env, argv) => { ? `https://${process.env.CAPACITOR_PRODUCTION_HOSTNAME}` : `http://${getLocalIP()}:9000` : ""; + const apiBaseForLocalhost = `http://${process.env.CAPACITOR_BUILD ? getLocalIP() : "localhost"}:8787`; return { entry: "./src/client/Main.ts", @@ -163,6 +164,7 @@ export default async (env, argv) => { "process.env.GAME_ENV": JSON.stringify(isProduction ? "prod" : "dev"), "process.env.GIT_COMMIT": JSON.stringify(gitCommit), "process.env.APP_BASE_URL": JSON.stringify(appBaseUrl), + "process.env.LOCAL_API_BASE_URL": JSON.stringify(apiBaseForLocalhost), "process.env.CAPACITOR_PRODUCTION_HOSTNAME": JSON.stringify( process.env.CAPACITOR_PRODUCTION_HOSTNAME, ),