From d077292bbce454329b831e43a406aceae8d1cf95 Mon Sep 17 00:00:00 2001 From: debu-2 Date: Fri, 2 Oct 2026 12:32:20 +0530 Subject: [PATCH] proxy routing issue' --- .env.example | 6 ++--- docker-compose.yml | 3 ++- frontend/.env.example | 4 ++-- .../src/features/develop/ApiExplorerPage.tsx | 2 +- .../features/testing/TestingCenterPage.tsx | 2 +- frontend/src/features/voice/VoicePage.tsx | 9 +------- frontend/src/lib/api.ts | 2 +- frontend/src/lib/apiBase.ts | 22 +++++++++++++++++++ frontend/vite.config.ts | 9 ++++++++ 9 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 frontend/src/lib/apiBase.ts diff --git a/.env.example b/.env.example index 287f2df..f550ad1 100644 --- a/.env.example +++ b/.env.example @@ -21,9 +21,9 @@ GOOGLE_REDIRECT_URI=http://localhost:8000/api/v1/auth/google/callback # ENVIRONMENT=production # FRONTEND_ORIGIN=https://urmiai.com # CORS_ORIGINS=https://urmiai.com,http://urmiai.com,https://www.urmiai.com,http://www.urmiai.com -# VITE_API_URL=https://api.urmiai.com -# GOOGLE_REDIRECT_URI=https://api.urmiai.com/api/v1/auth/google/callback -# VOICE_RELAY_PUBLIC_URL=wss://api.urmiai.com/api/v1/voice +# VITE_API_URL= +# GOOGLE_REDIRECT_URI=https://urmiai.com/api/v1/auth/google/callback +# VOICE_RELAY_PUBLIC_URL=wss://urmiai.com/api/v1/voice # Ultronsms gateway credentials (backend only) ULTRONSMS_BASE_URL=https://ultronsms.com diff --git a/docker-compose.yml b/docker-compose.yml index 0682eb7..cbf9a7e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,8 @@ services: ports: - "5173:5173" environment: - VITE_API_URL: ${VITE_API_URL:-http://localhost:8000} + VITE_API_URL: ${VITE_API_URL:-} + VITE_PROXY_TARGET: http://backend:8000 volumes: - ./frontend/src:/app/src - ./frontend/public:/app/public diff --git a/frontend/.env.example b/frontend/.env.example index 28ee441..67e2809 100644 --- a/frontend/.env.example +++ b/frontend/.env.example @@ -3,6 +3,6 @@ # # Vite only exposes variables prefixed with VITE_ to the browser. -# Backend API base URL (no trailing slash) +# Backend API base URL (no trailing slash). Leave empty behind Caddy+Vite proxy. VITE_API_URL=http://localhost:8000 -# Production: VITE_API_URL=https://api.urmiai.com +# Production (same-origin via Vite proxy): VITE_API_URL= diff --git a/frontend/src/features/develop/ApiExplorerPage.tsx b/frontend/src/features/develop/ApiExplorerPage.tsx index 4f6c524..fc8d00c 100644 --- a/frontend/src/features/develop/ApiExplorerPage.tsx +++ b/frontend/src/features/develop/ApiExplorerPage.tsx @@ -14,7 +14,7 @@ import { type VoiceCall, } from '../../lib/api' -const API_BASE_URL = import.meta.env.VITE_API_URL ?? 'http://localhost:8000' +import { API_BASE_URL } from '../../lib/apiBase' function FieldRow({ label, value }: { label: string; value: string }) { return ( diff --git a/frontend/src/features/testing/TestingCenterPage.tsx b/frontend/src/features/testing/TestingCenterPage.tsx index 6572553..46f8ee0 100644 --- a/frontend/src/features/testing/TestingCenterPage.tsx +++ b/frontend/src/features/testing/TestingCenterPage.tsx @@ -13,7 +13,7 @@ import { } from '../../lib/api' import { buildMessage, splitTemplate } from '../messaging/templateUtils' -const API_BASE_URL = import.meta.env.VITE_API_URL ?? 'http://localhost:8000' +import { API_BASE_URL } from '../../lib/apiBase' const OTP_TEMPLATE_TYPES = [ { type: 'general_verification', name: 'General Verification' }, diff --git a/frontend/src/features/voice/VoicePage.tsx b/frontend/src/features/voice/VoicePage.tsx index 6ae6a46..a9a9fff 100644 --- a/frontend/src/features/voice/VoicePage.tsx +++ b/frontend/src/features/voice/VoicePage.tsx @@ -2,16 +2,9 @@ import { Mic, MicOff, Phone, PhoneOutgoing, Radio, ShieldCheck } from 'lucide-re import { useCallback, useEffect, useRef, useState } from 'react' import { Link } from 'react-router-dom' import { numbersApi, voiceApi, type PhoneNumber, type VoiceApiDocs, type VoiceCall } from '../../lib/api' +import { API_BASE_URL, wsBaseUrl } from '../../lib/apiBase' import { UrmiConsoleAudio, extractStreamId, type AudioPermissionState } from './voiceAudio' -const API_BASE_URL = import.meta.env.VITE_API_URL ?? 'http://localhost:8000' - -function wsBaseUrl() { - const url = new URL(API_BASE_URL) - url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:' - return url.origin -} - function formatPhone(value: string) { if (value.length === 10) return `${value.slice(0, 5)} ${value.slice(5)}` return value diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 100aadb..a745fb3 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -1,4 +1,4 @@ -const API_BASE_URL = import.meta.env.VITE_API_URL ?? 'http://localhost:8000' +import { API_BASE_URL } from './apiBase' export type DashboardResponse = { metrics: { diff --git a/frontend/src/lib/apiBase.ts b/frontend/src/lib/apiBase.ts new file mode 100644 index 0000000..49abf34 --- /dev/null +++ b/frontend/src/lib/apiBase.ts @@ -0,0 +1,22 @@ +const configured = import.meta.env.VITE_API_URL + +/** + * API base URL for browser requests. + * - unset → local dev default (http://localhost:8000) + * - empty string → same origin (/api proxied by Vite behind Caddy) + * - https://... → explicit API host + */ +export const API_BASE_URL = + configured === undefined + ? 'http://localhost:8000' + : configured.replace(/\/$/, '') + +export function wsBaseUrl(): string { + if (API_BASE_URL) { + const url = new URL(API_BASE_URL) + url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:' + return url.origin + } + const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:' + return `${protocol}//${window.location.host}` +} diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 493d102..13678a7 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -1,6 +1,8 @@ import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' +const proxyTarget = process.env.VITE_PROXY_TARGET ?? 'http://localhost:8000' + // https://vite.dev/config/ export default defineConfig({ plugins: [react()], @@ -8,5 +10,12 @@ export default defineConfig({ host: true, // Required when Caddy (or another proxy) forwards urmiai.com to the dev server allowedHosts: ['urmiai.com', 'www.urmiai.com', 'localhost'], + // Same-origin API: browser calls urmiai.com/api/*, Vite forwards to backend + proxy: { + '/api': { target: proxyTarget, changeOrigin: true, ws: true }, + '/health': { target: proxyTarget, changeOrigin: true }, + '/docs': { target: proxyTarget, changeOrigin: true }, + '/openapi.json': { target: proxyTarget, changeOrigin: true }, + }, }, })