From d75f0c462651d934f661c2e0f2319ab26445c5d0 Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Mon, 24 Feb 2025 13:39:07 +0100 Subject: [PATCH] fix: configure docker compose and fastify cors with .env file for local and production environment --- .env | 7 +++++++ .gitignore | 3 ++- backend/server.ts | 2 +- docker-compose.yaml | 22 ++++++++++++---------- frontend/next.config.ts | 2 +- 5 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..e810029 --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +####### LOCAL DEVELOPMENT ####### +NODE_ENV=development +HOSTNAME=0.0.0.0 +FRONTEND_PORT=3000 +BACKEND_PORT=4000 +BACKEND=http://localhost:4000 +CORS_ALLOWED_ORIGIN=http://localhost:3000 \ No newline at end of file diff --git a/.gitignore b/.gitignore index d94bd76..f17e042 100644 --- a/.gitignore +++ b/.gitignore @@ -32,7 +32,8 @@ yarn-error.log* .pnpm-debug.log* # env files (can opt-in for committing if needed) -.env* +frontend/.env +backend/.env # vercel .vercel diff --git a/backend/server.ts b/backend/server.ts index 2a8bdf5..bd56005 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -14,7 +14,7 @@ import { removeBG } from "./src/routes/removebg.route"; const app = Fastify({ logger: true }); app.register(cors, { - origin: "*", + origin: process.env.CORS_ALLOWED_ORIGIN || "*", exposedHeaders: "Content-Disposition", methods: "POST", allowedHeaders: "Content-Type", diff --git a/docker-compose.yaml b/docker-compose.yaml index 86cf1d9..0769ed6 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,13 +4,14 @@ services: context: ./frontend dockerfile: Dockerfile container_name: werkzeugkiste-frontend + env_file: .env environment: - - NODE_ENV=production - - HOSTNAME=0.0.0.0 - - PORT=3000 - - backend_url=http://backend:4000 + - NODE_ENV=${NODE_ENV} + - HOSTNAME=${HOSTNAME} + - PORT=${FRONTEND_PORT} + - BACKEND_URL=${BACKEND} ports: - - "3000:3000" + - "${FRONTEND_PORT}:3000" restart: unless-stopped backend: @@ -18,13 +19,14 @@ services: context: ./backend dockerfile: Dockerfile container_name: werkzeugkiste-backend + env_file: .env environment: - - NODE_ENV=production - - HOSTNAME=0.0.0.0 - - PORT=4000 - - CORS_ORIGIN=http://frontend:3000 + - NODE_ENV=${NODE_ENV} + - HOSTNAME=${HOSTNAME} + - PORT=${BACKEND_PORT} + - CORS_ALLOWED_ORIGIN=${CORS_ALLOWED_ORIGIN} ports: - - "4000:4000" + - "${BACKEND_PORT}:4000" restart: unless-stopped networks: diff --git a/frontend/next.config.ts b/frontend/next.config.ts index b3a8590..53fdcdf 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -3,7 +3,7 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { output: "standalone", env: { - backend_url: "http://localhost:4000", + backend_url: process.env.BACKEND_URL || "http://localhost:4000", }, };