Skip to content

Commit f19bbfd

Browse files
db_enabled error fix
1 parent 1daba51 commit f19bbfd

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

server/src/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@ import ngrok from 'ngrok';
33
import dotenv from 'dotenv-mono';
44
import chalk from 'chalk';
55
import { startDockerContainer, stopDockerContainer } from 'database';
6-
import { app } from './server.js';
76

87
// If DEV is true then change the port we are running on, to prevent undesired caching
98
const BACKEND_PORT = process.env.NODE_ENV === 'dev' ? 8081 : 8080;
109

1110
dotenv.load({ path: '.env' });
1211
dotenv.load({ path: '.env.local' });
1312

13+
function parseBooleanEnv(value: string | undefined): boolean | undefined {
14+
if (value == undefined) return undefined;
15+
const normalized = value.trim().toLowerCase();
16+
if (normalized.length === 0) return undefined;
17+
if (['1', 'true', 'yes', 'on'].includes(normalized)) return true;
18+
if (['0', 'false', 'no', 'off'].includes(normalized)) return false;
19+
return undefined;
20+
}
21+
1422
const REMOTE = process.env.LOCATION === 'remote';
1523
const isDev = process.env.NODE_ENV === 'dev';
16-
const devUseDocker = ['1', 'true', 'yes', 'on'].includes(
17-
String(process.env.DEV_USE_DOCKER ?? '').toLowerCase()
18-
);
19-
const dbEnabled = !isDev || devUseDocker;
24+
const devUseDocker = parseBooleanEnv(process.env.DEV_USE_DOCKER) === true;
25+
const explicitDbEnabled = parseBooleanEnv(process.env.DB_ENABLED);
26+
const dbEnabled = explicitDbEnabled ?? (!isDev || devUseDocker);
2027
process.env.DB_ENABLED = dbEnabled ? 'true' : 'false';
2128

2229
let container: Awaited<ReturnType<typeof startDockerContainer>> | null = null;
@@ -33,6 +40,8 @@ if (dbEnabled) {
3340
);
3441
}
3542

43+
const { app } = await import('./server.js');
44+
3645
const server = app.listen(BACKEND_PORT, () => {
3746
console.log(chalk.green('Server running at http://localhost:' + BACKEND_PORT));
3847
});

server/src/server.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ import {
4040

4141
// import { MatchData } from 'requests';
4242

43+
function parseBooleanEnv(value: string | undefined): boolean | undefined {
44+
if (value == undefined) return undefined;
45+
const normalized = value.trim().toLowerCase();
46+
if (normalized.length === 0) return undefined;
47+
if (['1', 'true', 'yes', 'on'].includes(normalized)) return true;
48+
if (['0', 'false', 'no', 'off'].includes(normalized)) return false;
49+
return undefined;
50+
}
51+
4352
// If DEV is true then the app should forward requests to localhost:5173 instead of serving from /static
4453
const DEV = process.env.NODE_ENV === 'dev';
45-
const DEV_USE_DOCKER = ['1', 'true', 'yes', 'on'].includes(
46-
String(process.env.DEV_USE_DOCKER ?? '').toLowerCase()
47-
);
48-
const DB_ENABLED = process.env.NODE_ENV !== 'dev' || DEV_USE_DOCKER;
54+
const DEV_USE_DOCKER = parseBooleanEnv(process.env.DEV_USE_DOCKER) === true;
55+
const explicitDbEnabled = parseBooleanEnv(process.env.DB_ENABLED);
56+
const DB_ENABLED = explicitDbEnabled ?? (process.env.NODE_ENV !== 'dev' || DEV_USE_DOCKER);
4957
const currentDir = path.dirname(fileURLToPath(import.meta.url));
5058
const repoRoot = path.resolve(currentDir, '../..');
5159
const staticDir = path.resolve(currentDir, '../static');

server/src/status.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ const redPositions = redPositionsAll.slice(
3030
0,
3131
gameConfig.allianceSizeRobots.default
3232
);
33-
const DB_ENABLED = ['1', 'true', 'yes', 'on'].includes(
34-
String(process.env.DB_ENABLED ?? '').toLowerCase()
35-
);
33+
function parseBooleanEnv(value: string | undefined): boolean | undefined {
34+
if (value == undefined) return undefined;
35+
const normalized = value.trim().toLowerCase();
36+
if (normalized.length === 0) return undefined;
37+
if (['1', 'true', 'yes', 'on'].includes(normalized)) return true;
38+
if (['0', 'false', 'no', 'off'].includes(normalized)) return false;
39+
return undefined;
40+
}
41+
42+
const DEV_USE_DOCKER = parseBooleanEnv(process.env.DEV_USE_DOCKER) === true;
43+
const explicitDbEnabled = parseBooleanEnv(process.env.DB_ENABLED);
44+
const DB_ENABLED = explicitDbEnabled ?? (process.env.NODE_ENV !== 'dev' || DEV_USE_DOCKER);
3645

3746
function readMatchScheduleSafe(): MatchSchedule | undefined {
3847
try {

0 commit comments

Comments
 (0)