update aws deployment, have client get env from server

This commit is contained in:
Evan
2025-03-05 12:37:37 -08:00
parent be5157a25c
commit 2b26cfbbc9
15 changed files with 159 additions and 66 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ export async function createGameRunner(
clientID: ClientID,
callBack: (gu: GameUpdateViewData) => void,
): Promise<GameRunner> {
const config = getConfig(gameConfig, null);
const config = await getConfig(gameConfig, null);
const gameMap = await loadGameMap(gameConfig.gameMap);
const game = createGame(
gameMap.gameMap,
+42 -16
View File
@@ -22,21 +22,24 @@ import { GameMap, TileRef } from "../game/GameMap";
import { PlayerView } from "../game/GameView";
import { UserSettings } from "../game/UserSettings";
let cachedSC: ServerConfig = null;
export enum GameEnv {
Dev,
Preprod,
Prod,
}
export function getConfig(
export async function getConfig(
gameConfig: GameConfig,
userSettings: UserSettings | null = null,
): Config {
const sc = getServerConfig();
switch (process.env.GAME_ENV) {
case "dev":
): Promise<Config> {
const sc = await getServerConfigFromClient();
switch (sc.env()) {
case GameEnv.Dev:
return new DevConfig(sc, gameConfig, userSettings);
case "preprod":
case "prod":
case GameEnv.Preprod:
case GameEnv.Prod:
consolex.log("using prod config");
return new DefaultConfig(sc, gameConfig, userSettings);
default:
@@ -44,20 +47,43 @@ export function getConfig(
}
}
export function getServerConfig(): ServerConfig {
switch (process.env.GAME_ENV) {
export async function getServerConfigFromClient(): Promise<ServerConfig> {
if (cachedSC) {
return cachedSC;
}
const response = await fetch("/api/env");
if (!response.ok) {
throw new Error(
`Failed to fetch server config: ${response.status} ${response.statusText}`,
);
}
const config = await response.json();
// Log the retrieved configuration
console.log("Server config loaded:", config);
cachedSC = getServerConfig(config.game_env);
return cachedSC;
}
export function getServerConfigFromServer(): ServerConfig {
const gameEnv = process.env.GAME_ENV;
return getServerConfig(gameEnv);
}
function getServerConfig(gameEnv: string) {
switch (gameEnv) {
case "dev":
consolex.log("using dev config");
consolex.log("using dev server config");
return new DevServerConfig();
case "preprod":
consolex.log("using preprod config");
case "staging":
consolex.log("using preprod server config");
return preprodConfig;
case "prod":
default:
consolex.log("using prod config");
consolex.log("using prod server config");
return prodConfig;
// default:
// throw Error(`unsupported server configuration: ${process.env.GAME_ENV}`)
default:
throw Error(`unsupported server configuration: ${gameEnv}`);
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ import { pastelThemeDark } from "./PastelThemeDark";
export abstract class DefaultServerConfig implements ServerConfig {
numWorkers(): number {
return 2;
return 6;
}
abstract env(): GameEnv;
abstract discordRedirectURI(): string;
+3
View File
@@ -15,6 +15,9 @@ export class DevServerConfig extends DefaultServerConfig {
discordRedirectURI(): string {
return "http://localhost:3000/auth/callback";
}
numWorkers(): number {
return 2;
}
}
export class DevConfig extends DefaultConfig {
+3
View File
@@ -8,4 +8,7 @@ export const preprodConfig = new (class extends DefaultServerConfig {
discordRedirectURI(): string {
return "https://openfront.dev/auth/callback";
}
numWorkers(): number {
return 3;
}
})();