From 7cadc006d3ca00d70bba332734c00184722facaa Mon Sep 17 00:00:00 2001 From: dvirlabs Date: Thu, 5 Jun 2025 02:50:39 +0300 Subject: [PATCH] Fix connections between pods --- frontend/.env | 1 + frontend/Dockerfile | 18 ++++++------------ frontend/docker-entrypoint.sh | 11 +++++++++++ frontend/src/services/api.js | 10 ++++++---- frontend/vite.config.js | 8 -------- navix-helm/templates/frontend-deployment.yaml | 5 ++++- navix-helm/templates/frontend-ingress.yaml | 14 +++++++++----- 7 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 frontend/.env create mode 100644 frontend/docker-entrypoint.sh diff --git a/frontend/.env b/frontend/.env new file mode 100644 index 0000000..5934e2e --- /dev/null +++ b/frontend/.env @@ -0,0 +1 @@ +VITE_API_URL=http://localhost:8000 diff --git a/frontend/Dockerfile b/frontend/Dockerfile index a5f2b17..1ada1cc 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,26 +1,20 @@ # Stage 1: Build FROM node:20-alpine AS builder - WORKDIR /app - COPY package*.json ./ RUN npm install --legacy-peer-deps - COPY . . RUN npm run build -# Stage 2: Serve with nginx +# Stage 2: NGINX FROM nginx:alpine -# Remove default nginx static files RUN rm -rf /usr/share/nginx/html/* - -# Copy built assets from the builder stage COPY --from=builder /app/dist /usr/share/nginx/html -# Copy custom nginx config if needed -# COPY nginx.conf /etc/nginx/conf.d/default.conf +# Runtime env injection +COPY docker-entrypoint.sh /docker-entrypoint.sh +RUN chmod +x /docker-entrypoint.sh +RUN echo "window.env = {}" > /usr/share/nginx/html/env.js -EXPOSE 80 - -CMD ["nginx", "-g", "daemon off;"] +CMD ["/docker-entrypoint.sh"] diff --git a/frontend/docker-entrypoint.sh b/frontend/docker-entrypoint.sh new file mode 100644 index 0000000..09e117e --- /dev/null +++ b/frontend/docker-entrypoint.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -e + +# Generate env.js at runtime +cat < /usr/share/nginx/html/env.js +window.env = { + VITE_API_URL: "${VITE_API_URL}" +} +EOF + +exec nginx -g "daemon off;" diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index d9929ed..66bf39e 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -1,11 +1,13 @@ +const API_BASE = window?.env?.VITE_API_URL || ''; + export async function fetchSections() { - const res = await fetch('/apps'); + const res = await fetch(`${API_BASE}/apps`); if (!res.ok) throw new Error('Failed to fetch sections'); return res.json(); } export async function addAppToSection({ section, app }) { - const res = await fetch('/add_app', { + const res = await fetch(`${API_BASE}/add_app`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ section, app }) @@ -15,8 +17,8 @@ export async function addAppToSection({ section, app }) { } export async function getIconUrl(filename) { - const res = await fetch(`/icon/${filename}`); + const res = await fetch(`${API_BASE}/icon/${filename}`); if (!res.ok) throw new Error(`Failed to fetch icon for ${filename}`); const data = await res.json(); - return data.url; // ✅ must return the actual URL string + return data.url; } diff --git a/frontend/vite.config.js b/frontend/vite.config.js index ea8be0a..0466183 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -1,14 +1,6 @@ import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; -// https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], - server: { - proxy: { - '/apps': 'http://localhost:8000', - '/add_app': 'http://localhost:8000', - '/icon': 'http://localhost:8000', - } - } }); diff --git a/navix-helm/templates/frontend-deployment.yaml b/navix-helm/templates/frontend-deployment.yaml index 1dd733e..203a5f5 100644 --- a/navix-helm/templates/frontend-deployment.yaml +++ b/navix-helm/templates/frontend-deployment.yaml @@ -17,4 +17,7 @@ spec: image: "{{ .Values.frontend.image.repository }}:{{ .Values.frontend.image.tag }}" imagePullPolicy: {{ .Values.frontend.image.pullPolicy }} ports: - - containerPort: 80 \ No newline at end of file + - containerPort: 80 + env: + - name: VITE_API_URL + value: "http://navix-backend.{{ .Release.Namespace }}.svc.cluster.local:8000" diff --git a/navix-helm/templates/frontend-ingress.yaml b/navix-helm/templates/frontend-ingress.yaml index 55977e5..b1d61d4 100644 --- a/navix-helm/templates/frontend-ingress.yaml +++ b/navix-helm/templates/frontend-ingress.yaml @@ -10,14 +10,18 @@ metadata: spec: ingressClassName: {{ .Values.frontend.ingress.className }} rules: - - host: {{ .Values.frontend.ingress.hosts[0].host }} + {{- range .Values.frontend.ingress.hosts }} + - host: {{ .host }} http: paths: - - path: {{ .Values.frontend.ingress.hosts[0].paths[0].path }} - pathType: {{ .Values.frontend.ingress.hosts[0].paths[0].pathType }} + {{- range .paths }} + - path: {{ .path }} + pathType: {{ .pathType }} backend: service: name: navix-frontend port: - number: {{ .Values.frontend.service.port }} -{{- end }} \ No newline at end of file + number: {{ $.Values.frontend.service.port }} + {{- end }} + {{- end }} +{{- end }}