Made small refactoring

This commit is contained in:
oleksandr-shysh
2025-06-16 12:16:03 +03:00
parent 179eb6f72f
commit d9e49f3438
8 changed files with 34 additions and 21 deletions
+13 -2
View File
@@ -585,13 +585,24 @@ async function createLobby(): Promise<GameInfo> {
}
}
// Cache for storing computed game URLs
const urlCache = new Map<string, string>();
export async function buildGameUrl(
gameID: string,
path: string,
): Promise<string> {
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;
}
+1 -3
View File
@@ -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<boolean> {
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",
+1 -1
View File
@@ -57,7 +57,7 @@ export class PublicLobby extends LitElement {
async fetchLobbies(): Promise<GameInfo[]> {
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}`);
+5 -5
View File
@@ -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<void> {
@@ -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 {
+1 -1
View File
@@ -29,7 +29,7 @@ export async function getServerConfigFromClient(): Promise<ServerConfig> {
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(
+1 -1
View File
@@ -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<JWK> {
+10 -8
View File
@@ -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<string>([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,
+2
View File
@@ -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,
),