refactor WebSocket and fetch API URLs to use environment variables for better configuration

This commit is contained in:
ishikabhoyar
2025-11-03 14:25:01 +05:30
parent 6ef2edb768
commit 2508731ec7
2 changed files with 13 additions and 2 deletions

View File

@@ -339,7 +339,10 @@ int main() {
}
console.log('Creating new WebSocket connection');
const wsUrl = `ws://localhost:8080/api/ws/terminal/${id}`;
const apiUrl = import.meta.env.VITE_API_URL || 'http://localhost:8080';
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsBaseUrl = apiUrl.replace(/^https?:\/\//, '');
const wsUrl = `${wsProtocol}//${wsBaseUrl}/api/ws/terminal/${id}`;
const socket = new WebSocket(wsUrl);
socket.onopen = () => {
@@ -515,7 +518,8 @@ int main() {
try {
// Submit code to the backend
const response = await fetch('http://localhost:8080/api/submit', {
const apiUrl = import.meta.env.VITE_API_URL || 'http://localhost:8080';
const response = await fetch(`${apiUrl}/api/submit`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',