Try fix dynamic env

This commit is contained in:
dvirlabs 2025-07-02 04:40:10 +03:00
parent a26902e414
commit 6e55b088e9
7 changed files with 75 additions and 30 deletions

View File

@ -14,7 +14,6 @@ router = APIRouter()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

View File

@ -0,0 +1,4 @@
#!/bin/sh
# Generate env.js from the template
envsubst < /etc/env/env.js.template > /usr/share/nginx/html/env.js

View File

@ -1,29 +1,29 @@
# Stage 1: Build
FROM node:20-alpine AS builder
# Stage 1: Build the frontend
FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install --legacy-peer-deps
COPY . .
RUN npm install
RUN npm run build
# Stage 2: Runtime (NGINX)
# Stage 2: Serve with nginx
FROM nginx:alpine
# Install dos2unix to fix Windows line endings
# Install dos2unix
RUN apk add --no-cache dos2unix
# Clean default nginx html
RUN rm -rf /usr/share/nginx/html/*
# Copy built frontend
# Copy built app
COPY --from=builder /app/dist /usr/share/nginx/html
# Copy entrypoint and normalize line endings
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN dos2unix /docker-entrypoint.sh && chmod +x /docker-entrypoint.sh
# ✅ Copy env.js.template
COPY public/env.js.template /etc/env/env.js.template
# Fallback env.js (to avoid 404 before initContainer writes real one)
RUN echo "window.env = {}" > /usr/share/nginx/html/env.js
# ✅ Copy nginx config
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Set entrypoint
ENTRYPOINT ["/docker-entrypoint.sh"]
# ✅ Add env generator script to nginx entrypoint hook
COPY 10-generate-env.sh /docker-entrypoint.d/10-generate-env.sh
RUN dos2unix /docker-entrypoint.d/10-generate-env.sh && chmod +x /docker-entrypoint.d/10-generate-env.sh
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

View File

@ -1,11 +0,0 @@
#!/bin/sh
set -e
# Generate env.js at runtime from env var
cat <<EOF > /usr/share/nginx/html/env.js
window.env = {
VITE_API_URL: "${VITE_API_URL}"
};
EOF
exec nginx -g "daemon off;"

View File

@ -1,4 +1,4 @@
const API_BASE = window?.env?.VITE_API_URL || '/api';
const API_BASE = window?.ENV?.API_BASE || "";
export async function fetchSections() {
const res = await fetch(`${API_BASE}/apps`);

View File

@ -1,6 +1,59 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
});
// export default defineConfig({
// plugins: [react()],
// server: {
// proxy: {
// '/apps': 'http://localhost:8000',
// '/add_app': 'http://localhost:8000',
// '/icon': 'http://localhost:8000',
// }
// }
// });
// import { defineConfig } from 'vite';
// import react from '@vitejs/plugin-react';
// export default defineConfig({
// plugins: [react()],
// });