Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ NEXTAUTH_SECRET="your-secret-goes-here"
NEXTAUTH_URL="http://localhost:3000"
DATABASE_URL=file:./dev.db
INGEST_CHALLENGES_AT_STARTUP=true
CHALLENGES_DIR="./challenges"
CHALLENGES_DIR="./challenges"
GAME_START_TIME=""
GAME_END_TIME=""
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ NEXTAUTH_SECRET="your-secret-here"
NEXTAUTH_URL="http://localhost:3000"
INGEST_CHALLENGES_AT_STARTUP=true
CHALLENGES_DIR="./challenges"
GAME_START_TIME=""
GAME_END_TIME=""
```

Set `INGEST_CHALLENGES_AT_STARTUP` to `true` if you want challenges in `CHALLENGES_DIR` automatically imported when the server starts.
`GAME_START_TIME` and `GAME_END_TIME` accept ISO 8601 date strings. When set, these values replace the existing start and end times in the database during server startup.

## 📝 License

Expand Down
4 changes: 2 additions & 2 deletions src/app/api/game-config/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function GET() {
isActive: false,
startTime: null,
endTime: null,
hasEndTime: true
hasEndTime: true,
});
}

Expand All @@ -25,7 +25,7 @@ export async function GET() {
...gameConfig,
startTime: gameConfig.startTime.toISOString(),
endTime: gameConfig.endTime?.toISOString() || null,
hasEndTime: gameConfig.endTime !== null
hasEndTime: gameConfig.endTime !== null,
});
} catch (error) {
console.error('Error fetching game config:', error);
Expand Down
33 changes: 33 additions & 0 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
import { prisma } from '@/lib/prisma';

export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
console.log('Running startup tasks...');

if (process.env.GAME_START_TIME || process.env.GAME_END_TIME) {
const envStart = process.env.GAME_START_TIME;
const envEnd = process.env.GAME_END_TIME;

const startDate = envStart ? new Date(envStart) : undefined;
const endDate = envEnd ? new Date(envEnd) : undefined;

if ((startDate && isNaN(startDate.getTime())) || (endDate && isNaN(endDate.getTime()))) {
console.error('Invalid GAME_START_TIME or GAME_END_TIME values');
} else {
const existingConfig = await prisma.gameConfig.findFirst();
if (existingConfig) {
await prisma.gameConfig.update({
where: { id: existingConfig.id },
data: {
...(startDate ? { startTime: startDate } : {}),
...(envEnd !== undefined ? { endTime: endDate ?? null } : {}),
},
});
} else if (startDate) {
await prisma.gameConfig.create({
data: {
startTime: startDate,
endTime: endDate ?? null,
isActive: true,
},
});
}
}
}

if (process.env.INGEST_CHALLENGES_AT_STARTUP === 'true') {
// Run challenge ingestion
const { ChallengeIngestionService } = await import('@/lib/challenge-ingestion');
Expand Down