diff --git a/.dockerignore b/.dockerignore index 96420b5d..f500df50 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,7 @@ docker-compose.yml Dockerfile node_modules +dist .github .vscode .git diff --git a/.gitignore b/.gitignore index 51765f56..e4250d43 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ Thumbs.db # Next.js .next .env.local +.env.flyio .env pm2.json diff --git a/docker-compose.yml b/docker-compose.yml index aef83a3b..5a2cc20e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,10 @@ services: backend: - container_name: backend + container_name: namviek-backend build: context: . - dockerfile: ./docker/Dockerfile + dockerfile: ./docker/backend/Dockerfile + # dockerfile: ./docker/Dockerfile args: - ENV_FILE=.env.local image: namviek @@ -14,8 +15,9 @@ services: - MONGODB_URL=mongodb://mongodb:27017/namviek?replicaSet=rs0&authSource=admin&directConnection=true - AWS_MINIO_ENDPOINT=http://localhost:9000 ports: - - '3333:3333' - command: /bin/sh -c "./docker/be-entrypoint.sh && yarn backend" + - '3333:3000' + # command: /bin/sh -c "./docker/be-entrypoint.sh && yarn backend" + command: /bin/sh -c "node ./main.js" healthcheck: test: curl --fail http://localhost:3333 || exit 1 interval: 30s @@ -29,13 +31,17 @@ services: - namviek-network frontend: - container_name: frontend - image: namviek + container_name: namviek-frontend + build: + context: . + dockerfile: ./docker/frontend/Dockerfile + # dockerfile: ./docker/Dockerfile env_file: - .env.local ports: - - '3000:3000' - command: /bin/sh -c "./docker/fe-entrypoint.sh && yarn prod:fe" + - '3005:3000' + # command: /bin/sh -c "./docker/fe-entrypoint.sh && yarn prod:fe" + command: /bin/sh -c 'HOSTNAME="0.0.0.0" node ./packages/ui-app/server.js' depends_on: - backend networks: diff --git a/docker/backend/Dockerfile b/docker/backend/Dockerfile new file mode 100644 index 00000000..30d8287b --- /dev/null +++ b/docker/backend/Dockerfile @@ -0,0 +1,67 @@ +# ----------------------- base ----------------- +FROM node:lts-alpine AS base + +USER root +# Update image +RUN apk update && apk upgrade --available && apk add --no-cache g++ make python3 + +COPY . /app +WORKDIR /app + +# Add NX +RUN yarn global add nx@latest + +# ----------------------- dependencies ----------------- +FROM base AS deps + +RUN npm install --only=development + +# ------------------------ builder --------------- +FROM base AS builder + +# add non root user +# RUN addgroup -S namviek && \ +# adduser -S -G namviek namviek + +WORKDIR /app + +# Argument to select the env file +ARG ENV_FILE=.env +# Copy the env file +COPY ${ENV_FILE} .env + +COPY --from=deps /app/node_modules ./node_modules + +RUN yarn generate2 +RUN yarn build:be + +# ------------------------ runner --------------------- +FROM node:lts-alpine AS runner + +WORKDIR /app + +COPY --from=builder /app/dist/packages/be-gateway ./ +COPY ./packages/shared-models/src/prisma/schema.prisma . +COPY ./yarn.lock . + +# Only need MONGODB_URL in .env file +COPY .env .env + +# RUN yarn install --frozen-lockfile +RUN yarn install + + +# ---------------- TEST ------------ +COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma +# -------------------------------- + + +ENV PORT=3000 + +EXPOSE 3000 + + +# CMD HOSTNAME="0.0.0.0" node main.js +CMD ["node", "main.js"] +# CMD ["node", "test.js"] + diff --git a/docker/backend/Dockerfile-flyio b/docker/backend/Dockerfile-flyio new file mode 100644 index 00000000..81380ded --- /dev/null +++ b/docker/backend/Dockerfile-flyio @@ -0,0 +1,47 @@ +# ----------------------- base ----------------- +FROM node:lts-alpine AS base + +USER root +# Update image +RUN apk update && apk upgrade --available && apk add --no-cache g++ make python3 + +COPY . /app +WORKDIR /app + +# Add NX +RUN yarn global add nx@latest + +# ----------------------- dependencies ----------------- +FROM base AS deps + +RUN npm install --only=development + +# ------------------------ builder --------------- +FROM base AS builder + +WORKDIR /app + +COPY --from=deps /app/node_modules ./node_modules + +RUN yarn generate2 +RUN yarn build:be + +# ------------------------ runner --------------------- +FROM node:lts-alpine AS runner + +WORKDIR /app + +COPY --from=builder /app/dist/packages/be-gateway ./ +COPY ./packages/shared-models/src/prisma/schema.prisma . +COPY ./yarn.lock . + +RUN yarn install + +COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma + +# Set both HOST and PORT explicitly +ENV HOST=0.0.0.0 +ENV PORT=3000 + +# Use a shell to start the app with explicit host and port +CMD node main.js diff --git a/docker/frontend/Dockerfile b/docker/frontend/Dockerfile new file mode 100644 index 00000000..abffd959 --- /dev/null +++ b/docker/frontend/Dockerfile @@ -0,0 +1,62 @@ +FROM node:lts-alpine AS base + +USER root +# Update image +RUN apk update && apk upgrade --available && apk add --no-cache g++ make python3 + +COPY . /app +WORKDIR /app + +# Add NX +RUN yarn global add nx@latest + +# ----------------------- dependencies ----------------- +FROM base AS deps + +# Install everything, I have no idea if we want --production +RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn \ + yarn install --immutable + + +# ----------------------- builder ----------------- + +FROM base AS builder + +# # add non root user +# RUN addgroup -S namviek && \ +# adduser -S -G namviek namviek + +WORKDIR /app + +# Argument to select the env file +ARG ENV_FILE=.env + +# Copy the env file +COPY ${ENV_FILE} .env + +COPY --from=deps /app/node_modules ./node_modules + +RUN yarn generate2 +RUN yarn build:fe + +# ----------------------- runner ----------------- +FROM node:lts-alpine AS runner + +# USER namviek +WORKDIR /app + +COPY --from=builder /app/dist/packages/ui-app/.next/standalone ./ +COPY --from=builder /app/dist/packages/ui-app/.next/static ./dist/packages/ui-app/.next/static +COPY --from=builder /app/dist/packages/ui-app/public ./packages/ui-app/public + +RUN chmod -R 0777 * + +# Set environment variables +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 +ENV PORT=3000 + +# Expose the port the app runs on +EXPOSE 3000 + +CMD HOSTNAME="0.0.0.0" node ./packages/ui-app/server.js diff --git a/docker/test.js b/docker/test.js new file mode 100644 index 00000000..428f1456 --- /dev/null +++ b/docker/test.js @@ -0,0 +1,8 @@ +const loop = () => { + console.log('log', new Date()) + setTimeout(() => { + loop() + }, 5000) +} + +loop() diff --git a/fly.toml b/fly.toml new file mode 100644 index 00000000..918d0d27 --- /dev/null +++ b/fly.toml @@ -0,0 +1,29 @@ +app = 'namviek' +primary_region = 'sin' + +[build] + dockerfile = 'docker/backend/Dockerfile-flyio' + +[env] + JWT_REFRESH_EXPIRED = '2d' + JWT_TOKEN_EXPIRED = '30m' + JWT_VERIFY_USER_LINK_TOKEN_EXPIRED = '10m' + HOST = '0.0.0.0' + PORT = '3000' + +[http_service] + internal_port = 3000 + force_https = true + auto_stop_machines = true + auto_start_machines = true + min_machines_running = 1 + processes = ['app'] + +[scale] + count = 1 + max = 1 + +[[vm]] + cpu_kind = 'shared' + cpus = 1 + memory_mb = 1024 diff --git a/nx.json b/nx.json index d4fc0db8..19253d1e 100644 --- a/nx.json +++ b/nx.json @@ -4,7 +4,18 @@ "affected": { "defaultBase": "master" }, + "pluginsConfig": { + "@nx/js": { + "analyzeSourceFiles": true + } + }, "targetDefaults": { + "build": { + "executor": "@nx/webpack:webpack", + "options": { + "generatePackageJson": true + } + }, "test": { "inputs": ["default", "^default", "{workspaceRoot}/jest.preset.js"] }, diff --git a/package.json b/package.json index 1b6e981e..8ce4cc47 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,7 @@ "seed": "prisma db seed --schema=.\\packages\\shared-models\\src\\prisma\\schema.prisma", "generate2": "prisma generate --schema=./packages/shared-models/src/prisma/schema.prisma", "pushdb2": "prisma db push --schema=./packages/shared-models/src/prisma/schema.prisma", - "seed2": "prisma db seed --schema=./packages/shared-models/src/prisma/schema.prisma", - "postinstall": "yarn generate2" + "seed2": "prisma db seed --schema=./packages/shared-models/src/prisma/schema.prisma" }, "private": true, "dependencies": { diff --git a/packages/be-gateway/project.json b/packages/be-gateway/project.json index 12cfed3a..f5fe51b0 100644 --- a/packages/be-gateway/project.json +++ b/packages/be-gateway/project.json @@ -16,7 +16,8 @@ "tsConfig": "packages/be-gateway/tsconfig.app.json", "assets": ["packages/be-gateway/src/assets"], "isolatedConfig": true, - "webpackConfig": "packages/be-gateway/webpack.config.js" + "webpackConfig": "packages/be-gateway/webpack.config.js", + "generatePackageJson": true }, "configurations": { "development": {}, diff --git a/packages/ui-app/next.config.js b/packages/ui-app/next.config.js index d2ef6547..27eeccd7 100644 --- a/packages/ui-app/next.config.js +++ b/packages/ui-app/next.config.js @@ -7,6 +7,7 @@ const { composePlugins, withNx } = require('@nx/next') * @type {import('@nx/next/plugins/with-nx').WithNxOptions} **/ const nextConfig = { + output: 'standalone', nx: { // Set this to true if you would like to use SVGR // See: https://github.com/gregberge/svgr diff --git a/tsconfig.base.json b/tsconfig.base.json index 4d880d4b..672353ed 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -16,9 +16,9 @@ "baseUrl": ".", "paths": { "@/components/*": ["packages/ui-app/app/_components/*"], + "@/events/*": ["packages/ui-app/app/_events/*"], "@/features/*": ["packages/ui-app/app/_features/*"], "@/hooks/*": ["packages/ui-app/app/_hooks/*"], - "@/events/*": ["packages/ui-app/app/_events/*"], "@/services/*": ["packages/ui-app/services/*"], "@/store/*": ["packages/ui-app/store/*"], "@be/storage": ["packages/be-storage/src/index.ts"],