diff --git a/.gitignore b/.gitignore index e8469e2..9ddc667 100644 --- a/.gitignore +++ b/.gitignore @@ -46,6 +46,7 @@ node_modules/ .expo/ .yarn/ .env +.env.production dist/ npm-debug.* *.jks diff --git a/apps/api/Dockerfile.dev b/apps/api/Dockerfile.dev index 58877c1..b77b154 100644 --- a/apps/api/Dockerfile.dev +++ b/apps/api/Dockerfile.dev @@ -6,11 +6,9 @@ WORKDIR /app # Copy package files and install dependencies COPY package*.json ./ RUN npm install --legacy-peer-deps -# RUN npm run build:api # Copy the project files COPY . . -# RUN npx nx run api:build # Expose the Node.js server port EXPOSE 3333 diff --git a/apps/api/Dockerfile.prod b/apps/api/Dockerfile.prod index e6e2d09..2b03ebb 100644 --- a/apps/api/Dockerfile.prod +++ b/apps/api/Dockerfile.prod @@ -4,11 +4,15 @@ FROM node:20.18.0-alpine AS builder # Set working directory WORKDIR /app +ENV HUSKY_SKIP_INSTALL=1 + # Copy package files and install dependencies -COPY package*.json ./ -RUN npm install --legacy-peer-deps +COPY package.json package-lock.json ./ +RUN npm ci --omit=dev +RUN npm ci nx@19.5.6 # Copy the project files +COPY .env.production .env COPY . . RUN npx nx run api:build @@ -22,8 +26,8 @@ COPY --from=builder /app/dist/apps/api ./dist COPY package*.json ./ # Install a lightweight HTTP server to serve the static files -RUN npm install --legacy-peer-deps -RUN npm install prune --legacy-peer-deps +RUN npm ci +#--legacy-peer-deps # Expose the port the app runs on EXPOSE 3333 diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 5dd0ee1..71ab691 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -12,7 +12,18 @@ const app = express(); app.set('trust proxy', true); app.use(express.json()); app.use(express.urlencoded({ extended: true })); -app.use(cors()); +const whitelist = ['http://localhost:4200', 'http://localhost:80', 'http://localhost:3333']; +app.use( + cors({ + origin: function (origin, callback) { + if (whitelist.indexOf(origin) !== -1) { + callback(null, true); + } else { + callback(new Error('Not allowed by CORS')); + } + }, + }) +); app.use(cacheMiddleware); // Using Routes diff --git a/apps/web-app/Dockerfile.dev b/apps/web-app/Dockerfile.dev index cb0acbe..64c2136 100644 --- a/apps/web-app/Dockerfile.dev +++ b/apps/web-app/Dockerfile.dev @@ -5,7 +5,6 @@ WORKDIR /app # Copy package files and install dependencies COPY package*.json ./ -# COPY tools/scripts/postinstall.sh ./tools/scripts/postinstall.sh RUN npm install --legacy-peer-deps # Copy the project files diff --git a/apps/web-app/Dockerfile.prod b/apps/web-app/Dockerfile.prod index f02a2ab..4b18af5 100644 --- a/apps/web-app/Dockerfile.prod +++ b/apps/web-app/Dockerfile.prod @@ -4,14 +4,17 @@ FROM node:20.18.0-alpine AS builder # Set working directory WORKDIR /app +ENV HUSKY_SKIP_INSTALL=1 + # Copy package files and install dependencies -COPY package*.json ./ -RUN npm install --legacy-peer-deps -RUN npm install prune --legacy-peer-deps +COPY package.json package-lock.json ./ +RUN npm ci --omit=dev +RUN npm ci nx@19.5.6 # Copy the project files +COPY .env.production .env COPY . . -RUN npx nx run web-app:build +RUN npx nx run web-app:build -- --mode production # Stage 2: Serve FROM nginx:alpine AS production diff --git a/apps/web-app/vite.config.ts b/apps/web-app/vite.config.ts index adb5b0d..7fac6a2 100644 --- a/apps/web-app/vite.config.ts +++ b/apps/web-app/vite.config.ts @@ -1,11 +1,19 @@ /// import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; import react from '@vitejs/plugin-react'; +import dotenv from 'dotenv'; import { defineConfig } from 'vite'; +dotenv.config(); + +console.log('process.env', process.env); + export default defineConfig({ root: __dirname, cacheDir: '../../node_modules/.vite/apps/web-app', + // define: { + // 'process.env': process.env, + // }, server: { port: 4200, diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 7f59027..75f2021 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -8,8 +8,6 @@ services: - /app/node_modules ports: - '4200:4200' - environment: - - NODE_ENV=development depends_on: - api @@ -22,5 +20,3 @@ services: - /app/node_modules ports: - '3333:3333' - environment: - - NODE_ENV=development diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 5c2755f..6c56b67 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -5,8 +5,8 @@ services: dockerfile: ./apps/web-app/Dockerfile.prod ports: - '4200:80' - environment: - - NODE_ENV=production + env_file: + - .env.production depends_on: - api @@ -16,5 +16,5 @@ services: dockerfile: ./apps/api/Dockerfile.prod ports: - '3333:3333' - environment: - - NODE_ENV=production + env_file: + - .env.production diff --git a/package-lock.json b/package-lock.json index 1263abf..c9463fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,6 @@ "": { "name": "@pokemon-pet-shop/source", "version": "0.0.0", - "hasInstallScript": true, "license": "MIT", "dependencies": { "@expo/metro-config": "~0.18.1", @@ -19,6 +18,7 @@ "axios": "^1.6.0", "classnames": "^2.5.1", "cors": "^2.8.5", + "dotenv": "^16.4.5", "eslint-plugin-prettier": "^5.2.1", "expo": "^51.0.37", "expo-linear-gradient": "~13.0.2", diff --git a/package.json b/package.json index 330fc08..29b1871 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.0", "license": "MIT", "scripts": { - "prepare": "husky", + "prepare": "if [ \"$NODE_ENV\" = \"development\" ]; then husky install; fi", "lint-staged": "npx lint-staged --verbose", "docker-web-build:dev": "docker-compose -f docker-compose.dev.yml build", "docker-web-up:dev": "docker-compose -f docker-compose.dev.yml up", @@ -35,6 +35,7 @@ "axios": "^1.6.0", "classnames": "^2.5.1", "cors": "^2.8.5", + "dotenv": "^16.4.5", "eslint-plugin-prettier": "^5.2.1", "expo": "^51.0.37", "expo-linear-gradient": "~13.0.2",