diff --git a/.env b/.env new file mode 100644 index 0000000..e0099a3 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +VITE_API_URL=http://localhost:3001 diff --git a/Dockerfile b/Dockerfile index 7fef6b1..8bfa000 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,11 @@ COPY package*.json ./ # On installe les dépendances RUN npm ci COPY . . + +# On récupère la variable VITE_API_URL injectée par Coolify pendant le build +ARG VITE_API_URL +ENV VITE_API_URL=$VITE_API_URL + # On lance le build (en ignorant les erreurs TypeScript si tu as modifié le package.json comme convenu) RUN npm run build diff --git a/scripts/replace_url.cjs b/scripts/replace_url.cjs new file mode 100644 index 0000000..059f879 --- /dev/null +++ b/scripts/replace_url.cjs @@ -0,0 +1,32 @@ +const fs = require('fs'); +const path = require('path'); +const dir = 'c:\\Users\\Goura\\AntigravityProjects\\LogManager\\frontend\\src'; + +function replaceInFile(filePath) { + let content = fs.readFileSync(filePath, 'utf8'); + let original = content; + + // Pattern 1: single quotes + content = content.replace(/'http:\/\/localhost:3001([^']*)'/g, "`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}$1`"); + + // Pattern 2: backticks (template literals) + // Avoid double injecting if already replaced + content = content.replace(/`http:\/\/localhost:3001([^`]*)`/g, "`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}$1`"); + + if (content !== original) { + fs.writeFileSync(filePath, content); + console.log("Updated", filePath); + } +} + +function walk(dir) { + const list = fs.readdirSync(dir); + for (let file of list) { + file = path.resolve(dir, file); + const stat = fs.statSync(file); + if (stat && stat.isDirectory()) walk(file); + else if (file.endsWith('.ts') || file.endsWith('.tsx')) replaceInFile(file); + } +} + +walk(dir); diff --git a/src/components/AIChatSidebar.tsx b/src/components/AIChatSidebar.tsx index 380cab5..6e96523 100644 --- a/src/components/AIChatSidebar.tsx +++ b/src/components/AIChatSidebar.tsx @@ -41,7 +41,7 @@ export default function AIChatSidebar({ isOpen, onClose, currentService }: AICha const fetchConversations = async () => { try { - const res = await fetch('http://localhost:3001/api/ai/conversations', { credentials: 'include' }); + const res = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/ai/conversations`, { credentials: 'include' }); const data = await res.json(); setConversations(data); // Si aucune conv n'est sélectionnée, prendre la plus récente @@ -55,7 +55,7 @@ export default function AIChatSidebar({ isOpen, onClose, currentService }: AICha const fetchMessages = async (id: number) => { try { - const res = await fetch(`http://localhost:3001/api/ai/conversations/${id}`, { credentials: 'include' }); + const res = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/ai/conversations/${id}`, { credentials: 'include' }); const data = await res.json(); setMessages(data.messages || []); } catch (err) { @@ -66,7 +66,7 @@ export default function AIChatSidebar({ isOpen, onClose, currentService }: AICha const startNewChat = async () => { try { const title = currentService ? `Analyse ${currentService.name}` : "Nouvelle discussion"; - const res = await fetch('http://localhost:3001/api/ai/conversations', { + const res = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/ai/conversations`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', @@ -86,7 +86,7 @@ export default function AIChatSidebar({ isOpen, onClose, currentService }: AICha const deleteConversation = async (e: React.MouseEvent, id: number) => { e.stopPropagation(); try { - await fetch(`http://localhost:3001/api/ai/conversations/${id}`, { + await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/ai/conversations/${id}`, { method: 'DELETE', credentials: 'include' }); @@ -127,7 +127,7 @@ export default function AIChatSidebar({ isOpen, onClose, currentService }: AICha abortControllerRef.current = abortController; try { - const response = await fetch('http://localhost:3001/api/ai/chat-stream', { + const response = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/ai/chat-stream`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', diff --git a/src/components/AddServiceModal.tsx b/src/components/AddServiceModal.tsx index 38fc7d3..f77ba9c 100644 --- a/src/components/AddServiceModal.tsx +++ b/src/components/AddServiceModal.tsx @@ -18,7 +18,7 @@ export default function AddServiceModal({ onClose, onAdded }: { onClose: () => v setFormData(prev => ({ ...prev, pathOrContainerId: '' })); try { const endpoint = formData.type === 'docker' ? '/api/docker/containers' : '/api/os/services'; - const res = await fetch(`http://localhost:3001${endpoint}`, { + const res = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}${endpoint}`, { credentials: 'include' }); const data = await res.json(); @@ -35,7 +35,7 @@ export default function AddServiceModal({ onClose, onAdded }: { onClose: () => v const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); try { - await fetch('http://localhost:3001/api/services', { + await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/services`, { method: 'POST', headers: { 'Content-Type': 'application/json' diff --git a/src/components/LogTerminal.tsx b/src/components/LogTerminal.tsx index 436781a..f71ace9 100644 --- a/src/components/LogTerminal.tsx +++ b/src/components/LogTerminal.tsx @@ -20,7 +20,7 @@ export default function LogTerminal({ service, initialLogs = [] }: { useEffect(() => { if (!socketRef.current) { - socketRef.current = io('http://localhost:3001', { + socketRef.current = io(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}`, { withCredentials: true }); } diff --git a/src/components/SettingsModal.tsx b/src/components/SettingsModal.tsx index a4a2043..7cfb7b2 100644 --- a/src/components/SettingsModal.tsx +++ b/src/components/SettingsModal.tsx @@ -12,7 +12,7 @@ export default function SettingsModal({ onClose }: { onClose: () => void }) { const [saved, setSaved] = useState(false); useEffect(() => { - fetch('http://localhost:3001/api/settings', { + fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/settings`, { credentials: 'include' }) .then(res => res.json()) @@ -33,7 +33,7 @@ export default function SettingsModal({ onClose }: { onClose: () => void }) { const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); try { - await fetch('http://localhost:3001/api/settings', { + await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/settings`, { method: 'POST', headers: { 'Content-Type': 'application/json' diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index 975c894..db8969f 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -23,7 +23,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children useEffect(() => { const checkSession = async () => { try { - const res = await fetch('http://localhost:3001/api/auth/me', { + const res = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/auth/me`, { credentials: 'include' }); if (res.ok) { @@ -49,7 +49,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children const logout = async () => { try { - await fetch('http://localhost:3001/api/auth/logout', { + await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/auth/logout`, { method: 'POST', credentials: 'include' }); diff --git a/src/views/HomeView.tsx b/src/views/HomeView.tsx index 579d87f..f6a7d96 100644 --- a/src/views/HomeView.tsx +++ b/src/views/HomeView.tsx @@ -12,7 +12,7 @@ export default function HomeView() { const fetchServices = async () => { try { - const res = await fetch('http://localhost:3001/api/services', { + const res = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/services`, { credentials: 'include' }); const data = await res.json(); diff --git a/src/views/LoginView.tsx b/src/views/LoginView.tsx index fd67e4c..3802802 100644 --- a/src/views/LoginView.tsx +++ b/src/views/LoginView.tsx @@ -18,7 +18,7 @@ export default function LoginView() { setError(''); try { - const res = await fetch('http://localhost:3001/api/auth/login', { + const res = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', diff --git a/src/views/ServiceView.tsx b/src/views/ServiceView.tsx index d5d6e90..af9f944 100644 --- a/src/views/ServiceView.tsx +++ b/src/views/ServiceView.tsx @@ -36,10 +36,10 @@ export default function ServiceView() { const fetchServiceAndLogs = async () => { try { const [svcRes, logsRes] = await Promise.all([ - fetch(`http://localhost:3001/api/services`, { + fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/services`, { credentials: 'include' }), - fetch(`http://localhost:3001/api/services/${id}/logs`, { + fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/services/${id}/logs`, { credentials: 'include' }) ]); @@ -76,7 +76,7 @@ export default function ServiceView() { const handleDelete = async () => { try { - await fetch(`http://localhost:3001/api/services/${id}`, { + await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/services/${id}`, { method: 'DELETE', credentials: 'include' }); diff --git a/src/views/SignupView.tsx b/src/views/SignupView.tsx index aa6c12f..9992fb0 100644 --- a/src/views/SignupView.tsx +++ b/src/views/SignupView.tsx @@ -24,7 +24,7 @@ export default function SignupView() { setError(''); try { - const res = await fetch('http://localhost:3001/api/auth/register', { + const res = await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:3001'}/api/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include',