feat(docker-compose.yml): add VITE_PROXY_TARGET environment variable to frontend service

feat(frontend/dev.Dockerfile): change npm start command to npm run dev:docker
feat(frontend/package.json): add dev:docker script to run vite with host 0.0.0.0
feat(frontend/vite.config.ts): use environment variable to determine the target for proxying requests
This commit is contained in:
Gabriel Almeida 2023-05-15 19:08:49 -03:00
commit b0ded58d1e
4 changed files with 31 additions and 30 deletions

View file

@ -17,6 +17,8 @@ services:
dockerfile: ./dev.Dockerfile dockerfile: ./dev.Dockerfile
args: args:
- BACKEND_URL=http://backend:7860 - BACKEND_URL=http://backend:7860
environment:
- VITE_PROXY_TARGET=http://backend:7860
ports: ports:
- "3000:3000" - "3000:3000"
volumes: volumes:

View file

@ -23,4 +23,4 @@ RUN chmod +x set_proxy.sh && \
USER node USER node
RUN npm install --loglevel warn RUN npm install --loglevel warn
CMD ["npm", "start"] CMD ["npm", "run", "dev:docker"]

View file

@ -35,6 +35,7 @@
"web-vitals": "^2.1.4" "web-vitals": "^2.1.4"
}, },
"scripts": { "scripts": {
"dev:docker": "vite --host 0.0.0.0",
"start": "vite", "start": "vite",
"build": "vite build", "build": "vite build",
"serve": "vite preview" "serve": "vite preview"

View file

@ -1,34 +1,32 @@
import { defineConfig } from 'vite'; import { defineConfig } from "vite";
import react from '@vitejs/plugin-react-swc'; import react from "@vitejs/plugin-react-swc";
const apiRoutes = [ const apiRoutes = ["/all", "/predict", "^/validate/*", "^/chat/*"];
'/all',
'/predict', // Use environment variable to determine the target.
'^/validate/*', const target = process.env.VITE_PROXY_TARGET || "http://127.0.0.1:7860";
'^/chat/*',
];
const proxyTargets = apiRoutes.reduce((proxyObj, route) => { const proxyTargets = apiRoutes.reduce((proxyObj, route) => {
proxyObj[route] = { proxyObj[route] = {
target: 'http://127.0.0.1:7860', target: target,
changeOrigin: true, changeOrigin: true,
secure: false, secure: false,
ws: true, ws: true,
}; };
return proxyObj; return proxyObj;
}, {}); }, {});
export default defineConfig(() => { export default defineConfig(() => {
return { return {
build: { build: {
outDir: 'build', outDir: "build",
}, },
plugins: [react()], plugins: [react()],
server: { server: {
port: 3000, port: 3000,
proxy: { proxy: {
...proxyTargets ...proxyTargets,
} },
}, },
}; };
}); });