fix: Configure admin service URLs dynamically based on environment
All checks were successful
build-and-deploy / build (push) Successful in 9s
build-and-deploy / deploy (push) Successful in 11s
build-and-deploy / filter (push) Successful in 2s

- Update adminService to fetch server URL from /api/config endpoint
- Initialize server URL before making any API calls
- Make connect() method async to handle URL initialization
- Update Vue component to await adminService.connect()
- Resolves hardcoded localhost URLs in production environment
This commit is contained in:
2025-07-05 16:51:11 -06:00
parent f3d1365588
commit 5b6b05f840
2 changed files with 27 additions and 2 deletions

View File

@@ -129,7 +129,7 @@ onMounted(async () => {
serverUrl.value = config.serverUrl serverUrl.value = config.serverUrl
// Connect to SSE // Connect to SSE
adminService.connect((data) => { await adminService.connect((data) => {
if (data.type === 'connected') { if (data.type === 'connected') {
isConnected.value = true isConnected.value = true
} else if (data.type === 'gameStats') { } else if (data.type === 'gameStats') {

View File

@@ -26,8 +26,26 @@ class AdminService {
private connectionCallback: ConnectionCallback | null = null private connectionCallback: ConnectionCallback | null = null
private isConnected = false private isConnected = false
private serverUrl: string = 'http://localhost:2567' // Default to Colyseus server private serverUrl: string = 'http://localhost:2567' // Default to Colyseus server
private initialized = false
connect(callback: AdminCallback): void { private async initializeServerUrl(): Promise<void> {
if (this.initialized) return
try {
const response = await fetch('/api/config')
const config = await response.json()
this.serverUrl = config.serverUrl || 'http://localhost:2567'
this.initialized = true
} catch (error) {
console.warn('Failed to fetch server config, using default URL:', error)
this.serverUrl = 'http://localhost:2567'
this.initialized = true
}
}
async connect(callback: AdminCallback): Promise<void> {
await this.initializeServerUrl()
this.callback = callback this.callback = callback
this.eventSource = new EventSource('/api/sse') this.eventSource = new EventSource('/api/sse')
@@ -67,6 +85,7 @@ class AdminService {
// Admin control methods // Admin control methods
async kickPlayer(playerId: string): Promise<void> { async kickPlayer(playerId: string): Promise<void> {
await this.initializeServerUrl()
const response = await fetch(`${this.serverUrl}/api/admin/kick-player`, { const response = await fetch(`${this.serverUrl}/api/admin/kick-player`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@@ -79,6 +98,7 @@ class AdminService {
} }
async pauseGame(): Promise<void> { async pauseGame(): Promise<void> {
await this.initializeServerUrl()
const response = await fetch(`${this.serverUrl}/api/admin/pause-game`, { const response = await fetch(`${this.serverUrl}/api/admin/pause-game`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' } headers: { 'Content-Type': 'application/json' }
@@ -90,6 +110,7 @@ class AdminService {
} }
async resumeGame(): Promise<void> { async resumeGame(): Promise<void> {
await this.initializeServerUrl()
const response = await fetch(`${this.serverUrl}/api/admin/resume-game`, { const response = await fetch(`${this.serverUrl}/api/admin/resume-game`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' } headers: { 'Content-Type': 'application/json' }
@@ -101,6 +122,7 @@ class AdminService {
} }
async cancelGame(gameId: string): Promise<void> { async cancelGame(gameId: string): Promise<void> {
await this.initializeServerUrl()
const response = await fetch(`${this.serverUrl}/api/admin/cancel-game`, { const response = await fetch(`${this.serverUrl}/api/admin/cancel-game`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@@ -113,6 +135,7 @@ class AdminService {
} }
async kickAllPlayers(): Promise<void> { async kickAllPlayers(): Promise<void> {
await this.initializeServerUrl()
const response = await fetch(`${this.serverUrl}/api/admin/kick-all-players`, { const response = await fetch(`${this.serverUrl}/api/admin/kick-all-players`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' } headers: { 'Content-Type': 'application/json' }
@@ -124,6 +147,7 @@ class AdminService {
} }
async advanceRound(): Promise<void> { async advanceRound(): Promise<void> {
await this.initializeServerUrl()
const response = await fetch(`${this.serverUrl}/api/admin/advance-round`, { const response = await fetch(`${this.serverUrl}/api/admin/advance-round`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' } headers: { 'Content-Type': 'application/json' }
@@ -135,6 +159,7 @@ class AdminService {
} }
async previousRound(): Promise<void> { async previousRound(): Promise<void> {
await this.initializeServerUrl()
const response = await fetch(`${this.serverUrl}/api/admin/previous-round`, { const response = await fetch(`${this.serverUrl}/api/admin/previous-round`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' } headers: { 'Content-Type': 'application/json' }