Implement relative paths for frontend API and WebSocket connections

- Created `.env.production` file to define relative paths for production deployment.
- Updated frontend API and WebSocket client to use environment variables for dynamic URL handling.
- Enhanced deployment scripts (`deploy-ubuntu.sh`, `start.sh`, `start.bat`) to generate `.env.production` automatically.
- Added `PRODUCTION_URLS.md` for documentation on production URL configuration and troubleshooting.
This commit is contained in:
Primakov Alexandr Alexandrovich
2025-10-12 23:53:47 +03:00
parent 48fbb5bcb3
commit 70889421ea
7 changed files with 288 additions and 4 deletions

View File

@@ -1,7 +1,8 @@
import axios from 'axios';
import type { Repository, RepositoryCreate, Review, ReviewStats } from '../types';
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000/api';
// Используем относительный путь для production или env для development
const API_BASE_URL = import.meta.env.VITE_API_URL || '/api';
const api = axios.create({
baseURL: API_BASE_URL,

View File

@@ -1,7 +1,5 @@
import { WebSocketMessage } from '../types';
const WS_URL = import.meta.env.VITE_WS_URL || 'ws://localhost:8000';
export class WebSocketClient {
private ws: WebSocket | null = null;
private listeners: Map<string, Set<(data: any) => void>> = new Map();
@@ -9,13 +7,26 @@ export class WebSocketClient {
private maxReconnectAttempts = 5;
private reconnectDelay = 3000;
private getWebSocketUrl(): string {
// Если задан VITE_WS_URL, используем его
if (import.meta.env.VITE_WS_URL) {
return import.meta.env.VITE_WS_URL;
}
// Иначе определяем автоматически на основе текущего location
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const host = window.location.host;
return `${protocol}//${host}`;
}
connect() {
if (this.ws?.readyState === WebSocket.OPEN) {
return;
}
try {
this.ws = new WebSocket(`${WS_URL}/ws/reviews`);
const wsUrl = this.getWebSocketUrl();
this.ws = new WebSocket(`${wsUrl}/ws/reviews`);
this.ws.onopen = () => {
console.log('WebSocket connected');