fix: Configure admin service URLs dynamically based on environment
- 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:
@@ -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') {
|
||||||
|
|||||||
@@ -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' }
|
||||||
|
|||||||
Reference in New Issue
Block a user