Skip to content

Commit 884b6c4

Browse files
author
Mubarak Imam
committed
chore(ci): add docker scripts
- add dockerfile for backend - add docker-compose with backend details - update nestjs packages - add more excluded directories to tsconfig - add dummy query due to graphql constraint - add function to run pending migration at startup - update createJwtOptions on ConfigService
1 parent cd6b775 commit 884b6c4

File tree

11 files changed

+471
-252
lines changed

11 files changed

+471
-252
lines changed

.docker/docker-compose.debug.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '2.1'
2+
3+
services:
4+
nestpoc:
5+
image: nestpoc
6+
build: .
7+
environment:
8+
NODE_ENV: development
9+
ports:
10+
- 3000:3000
11+
- 9229:9229
12+
## set your startup file here
13+
command: node --inspect index.js

.docker/docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '3'
2+
3+
volumes:
4+
nestpoc_api:
5+
6+
services:
7+
backend:
8+
env_file: ../nestpoc-api/production.env
9+
build:
10+
context: ../nestpoc-api
11+
dockerfile: .docker/Dockerfile
12+
command: node /usr/nestpoc/dist/main.js
13+
volumes:
14+
- nestpoc_api:/home/node/app/node_modules
15+
- nestpoc_api:/usr/nestpoc/node_modules
16+
- ../nestpoc-api:/home/node/app
17+
ports:
18+
- '3000:3000'
19+
environment:
20+
PORT: 3000

nestpoc-api/.docker/Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# create a base image from node-alpine
2+
FROM node:12-alpine as base-server
3+
LABEL application="nestpoc-backend"
4+
ENV TERM=xterm-256color
5+
RUN npm install yarn -g
6+
7+
# branch off to another image just for building the app
8+
FROM base-server as builder
9+
ENV NODE_ENV=development
10+
ENV BUILDER_APP_DIR=/home/node/app
11+
RUN mkdir -p ${BUILDER_APP_DIR}/node_modules && chown -R node:node ${BUILDER_APP_DIR}
12+
WORKDIR ${BUILDER_APP_DIR}
13+
COPY package.json yarn.lock ./
14+
USER node
15+
RUN yarn install
16+
17+
# branch off to another image that will serve our app
18+
FROM base-server as server
19+
ENV SERVER_APP_DIR=/usr/nestpoc
20+
ENV BUILDER_APP_DIR=/home/node/app
21+
RUN mkdir -p ${SERVER_APP_DIR}/node_modules && chown -R node:node ${SERVER_APP_DIR}
22+
ENV NODE_ENV=production
23+
WORKDIR ${SERVER_APP_DIR}
24+
COPY --from=builder ${BUILDER_APP_DIR}/package.json ./
25+
USER node
26+
RUN yarn install --prod
27+
28+
# continue from where we stop in builder, build the app now
29+
FROM builder as final
30+
COPY --chown=node:node ./src ./src
31+
COPY --chown=node:node nest-cli.json tsconfig.json tsconfig.build.json ormconfig.json gulpfile.ts ./
32+
RUN yarn build
33+
34+
# continue from where we stopped in server, copy the build over
35+
FROM server
36+
ENV BUILDER_APP_DIR=/home/node/app
37+
COPY --chown=node:node --from=final ${BUILDER_APP_DIR}/dist ./dist

nestpoc-api/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
},
3434
"dependencies": {
3535
"@hapi/joi": "^14.3.1",
36-
"@nestjs/common": "^6.0.0",
37-
"@nestjs/core": "^6.0.0",
38-
"@nestjs/graphql": "^6.0.5",
39-
"@nestjs/jwt": "^6.0.0",
40-
"@nestjs/passport": "^6.0.0",
41-
"@nestjs/platform-express": "^6.0.0",
36+
"@nestjs/common": "^6.2.4",
37+
"@nestjs/core": "^6.2.4",
38+
"@nestjs/graphql": "^6.2.1",
39+
"@nestjs/jwt": "^6.1.0",
40+
"@nestjs/passport": "^6.1.0",
41+
"@nestjs/platform-express": "^6.2.4",
4242
"africastalking": "^0.4.0",
4343
"apollo-server-express": "^2.4.8",
4444
"bcryptjs": "^2.4.3",

nestpoc-api/src/graphql.schema.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ export class NestPocResponse {
4040
}
4141

4242
export abstract class IQuery {
43-
abstract temp__(): boolean | Promise<boolean>;
43+
abstract getUser(email?: string): UserResult | Promise<UserResult>;
4444
}
4545

4646
export class TwoFaResponse {
4747
secret: string;
4848
qr: string;
4949
}
50+
51+
export class UserResult {
52+
id: string;
53+
email?: string;
54+
username?: string;
55+
}

nestpoc-api/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ async function bootstrap() {
1414
const config = app.get<ConfigService>(ConfigService);
1515
await app.listen(config.port);
1616
}
17+
1718
bootstrap();

nestpoc-api/src/modules/common/config/config.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { databaseProviderKey, envProvider } from './constants';
77
export const databaseProvider = {
88
provide: databaseProviderKey,
99
useFactory: async (configService: ConfigService) => {
10-
return createConnection(configService.createDatabaseOpts());
10+
const conn = await createConnection(configService.createDatabaseOpts());
11+
await conn.runMigrations();
12+
return conn;
1113
},
1214
inject: [ConfigService],
1315
};
@@ -25,4 +27,4 @@ export const databaseProvider = {
2527
databaseProvider,
2628
],
2729
})
28-
export class ConfigModule { }
30+
export class ConfigModule {}

nestpoc-api/src/modules/common/config/config.service.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import * as Joi from '@hapi/joi';
22
import { Inject, Injectable } from '@nestjs/common';
33
import { GqlModuleOptions, GqlOptionsFactory } from '@nestjs/graphql';
44
import { JwtModuleOptions, JwtOptionsFactory } from '@nestjs/jwt';
5+
import { JwtSecretRequestType } from '@nestjs/jwt/dist/interfaces';
56
import * as dotenv from 'dotenv';
67
import * as fs from 'fs';
8+
import * as jwt from 'jsonwebtoken';
79
import * as path from 'path';
810

911
import { envProviderKey } from './constants';
@@ -105,15 +107,25 @@ export class ConfigService implements JwtOptionsFactory, GqlOptionsFactory {
105107
database: this.envConfig.NESTPOC_DB_STORE,
106108
entities: [entitiesPath],
107109
migrations: [migrationsPath],
108-
synchronize: true,
110+
synchronize: false,
109111
};
110112
}
111113

112114
public createJwtOptions(): JwtModuleOptions {
113115
return {
114-
secretOrPrivateKey: this.envConfig.NESTPOC_JWT_PRIVATE_KEY,
115-
signOptions: {
116-
expiresIn: '1d',
116+
secretOrKeyProvider: (
117+
requestType: JwtSecretRequestType,
118+
tokenOrPayload: string | Object | Buffer,
119+
verifyOrSignOrOptions?: jwt.VerifyOptions | jwt.SignOptions,
120+
) => {
121+
switch (requestType) {
122+
case JwtSecretRequestType.SIGN:
123+
return this.envConfig.NESTPOC_JWT_PRIVATE_KEY;
124+
case JwtSecretRequestType.VERIFY:
125+
return this.envConfig.NESTPOC_JWT_PUBLIC_KEY || this.envConfig.NESTPOC_JWT_PRIVATE_KEY;
126+
default:
127+
return this.envConfig.NESTPOC_JWT_PRIVATE_KEY;
128+
}
117129
},
118130
};
119131
}

nestpoc-api/src/modules/gateway/auth/auth.graphql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ type Mutation {
55
confirm2Fa(token: String): NestPocResponse!
66
}
77

8+
type Query {
9+
getUser(email: String): UserResult!
10+
}
11+
12+
type UserResult {
13+
id: String!
14+
email: String
15+
username: String
16+
}
17+
818
type LoginResponse {
919
success: Boolean!
1020
token: String

nestpoc-api/tsconfig.build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "./tsconfig.json",
3-
"exclude": ["node_modules", "test", "**/*spec.ts", "!src/**/*.ts"]
3+
"exclude": ["node_modules", "test", "**/*spec.ts", "!src/**/*.ts", "**/__mocks__"]
44
}

0 commit comments

Comments
 (0)