feat: implement game API with TRPC and Redis transport

- Added GameApiConfig interface and configuration resolver from environment variables.
- Created GameApiContext for managing database and transport dependencies.
- Implemented InMemoryTurnDaemonTransport for testing purposes.
- Developed RedisTurnDaemonTransport for command and status handling via Redis streams.
- Defined TurnDaemonStreamKeys for namespacing Redis streams by profile.
- Established TRPC router with endpoints for health check, world state retrieval, and turn daemon commands (run, pause, resume, status).
- Integrated Fastify server with TRPC and Redis transport.
- Added unit tests for router functionality and stream key generation.
- Configured Vitest for testing environment.
This commit is contained in:
2025-12-30 01:04:52 +00:00
parent d6c4a4ae25
commit 1688ca0d23
16 changed files with 1036 additions and 3 deletions
+42
View File
@@ -0,0 +1,42 @@
export interface GameApiConfig {
host: string;
port: number;
trpcPath: string;
profile: string;
scenario: string;
profileName: string;
daemonRequestTimeoutMs: number;
}
const parseNumber = (value: string | undefined, fallback: number, label: string): number => {
if (!value) {
return fallback;
}
const parsed = Number(value);
if (Number.isNaN(parsed)) {
throw new Error(`${label} must be a number.`);
}
return parsed;
};
export const resolveGameApiConfigFromEnv = (
env: NodeJS.ProcessEnv = process.env
): GameApiConfig => {
const profile = env.PROFILE ?? env.SERVER_PROFILE ?? 'che';
const scenario = env.SCENARIO ?? 'default';
const profileName = `${profile}:${scenario}`;
return {
host: env.GAME_API_HOST ?? '0.0.0.0',
port: parseNumber(env.GAME_API_PORT, 14000, 'GAME_API_PORT'),
trpcPath: env.TRPC_PATH ?? '/trpc',
profile,
scenario,
profileName,
daemonRequestTimeoutMs: parseNumber(
env.DAEMON_REQUEST_TIMEOUT_MS,
5000,
'DAEMON_REQUEST_TIMEOUT_MS'
),
};
};