From 8b46ade28b6b07f2932311d15725e9d727f67672 Mon Sep 17 00:00:00 2001 From: ahonn Date: Thu, 18 Jul 2024 20:40:12 +1000 Subject: [PATCH] build: add dockerfile/docker-compose files --- .dockerignore | 5 +++++ Dockerfile | 25 +++++++++++++++++++++++++ backend/package.json | 3 ++- backend/src/env.ts | 1 + backend/src/modules/api.module.ts | 5 +++-- docker-compose.yaml | 10 ++++++++++ 6 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..295f2278 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.git +.devbox +node_modules +backend/node_modules +frontend diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..0d5896f9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +ARG NODE_VERSION=20 + +FROM node:${NODE_VERSION}-slim AS base +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable +COPY . /app +WORKDIR /app + +FROM base AS prod-deps +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile + +FROM base as build +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile +RUN pnpm run --filter backend build + +FROM base +ENV NODE_ENV production +COPY --from=prod-deps /app/node_modules node_modules +COPY --from=prod-deps /app/package*.json . +COPY --from=prod-deps /app/backend/node_modules ./backend/node_modules +COPY --from=prod-deps /app/backend/package*.json ./backend +COPY --from=build /app/backend/dist ./backend/dist + +CMD ["node", "backend/dist/main.js"] diff --git a/backend/package.json b/backend/package.json index 3d09996f..be163a26 100644 --- a/backend/package.json +++ b/backend/package.json @@ -17,7 +17,8 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json" + "test:e2e": "jest --config ./test/jest-e2e.json", + "postinstall": "npx prisma generate" }, "dependencies": { "@apollo/server": "^4.10.4", diff --git a/backend/src/env.ts b/backend/src/env.ts index 7af221e1..a73d22ef 100644 --- a/backend/src/env.ts +++ b/backend/src/env.ts @@ -6,6 +6,7 @@ export const envSchema = z NETWORK: z .enum([NetworkType.mainnet, NetworkType.testnet]) .default(NetworkType.testnet), + ENABLED_GRAPHQL_PLAYGROUND: z.boolean().default(true), // DATABASE_URL: z.string(), // REDIS_URL: z.string(), diff --git a/backend/src/modules/api.module.ts b/backend/src/modules/api.module.ts index be84960e..bd963932 100644 --- a/backend/src/modules/api.module.ts +++ b/backend/src/modules/api.module.ts @@ -8,14 +8,15 @@ import { DataLoaderInterceptor } from '@applifting-io/nestjs-dataloader'; import { CkbModule } from './ckb/ckb.module'; import { RgbppModule } from './rgbpp/rgbpp.module'; import { BitcoinModule } from './bitcoin/bitcoin.module'; +import { Env } from 'src/env'; @Module({ imports: [ GraphQLModule.forRootAsync({ driver: ApolloDriver, inject: [ConfigService, SentryService], - useFactory: async (configService: ConfigService, sentryService: SentryService) => ({ - playground: configService.get('NODE_ENV') !== 'production', + useFactory: async (configService: ConfigService, sentryService: SentryService) => ({ + playground: configService.get('ENABLED_GRAPHQL_PLAYGROUND'), installSubscriptionHandlers: true, introspection: true, autoSchemaFile: true, diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..4eff4667 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,10 @@ +version: '3' + +services: + app: + build: + context: . + ports: + - '3000:3000' + env_file: + - ./backend/.env