feat: logger and dockerfile
This commit is contained in:
parent
1b645380e8
commit
7fdd03c093
21
.dockerignore
Normal file
21
.dockerignore
Normal file
|
@ -0,0 +1,21 @@
|
|||
# Versioning and metadata
|
||||
.git
|
||||
.gitignore
|
||||
.dockerignore
|
||||
|
||||
# Build dependencies
|
||||
dist
|
||||
node_modules
|
||||
coverage
|
||||
|
||||
# Environment (contains sensitive data)
|
||||
.env
|
||||
|
||||
# Files not required for production
|
||||
.editorconfig
|
||||
Dockerfile
|
||||
README.md
|
||||
tslint.json
|
||||
nodemon.json
|
||||
.idea
|
||||
k8s
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -35,3 +35,5 @@ lerna-debug.log*
|
|||
!.vscode/extensions.json
|
||||
|
||||
.env
|
||||
|
||||
k8s/**/secret.yaml
|
||||
|
|
45
Dockerfile
Normal file
45
Dockerfile
Normal file
|
@ -0,0 +1,45 @@
|
|||
# PRODUCTION DOCKERFILE
|
||||
# ---------------------
|
||||
# This Dockerfile allows to build a Docker image of the NestJS application
|
||||
# and based on a NodeJS 16 image. The multi-stage mechanism allows to build
|
||||
# the application in a "builder" stage and then create a lightweight production
|
||||
# image containing the required dependencies and the JS build files.
|
||||
#
|
||||
# Dockerfile best practices
|
||||
# https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
|
||||
# Dockerized NodeJS best practices
|
||||
# https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md
|
||||
# https://www.bretfisher.com/node-docker-good-defaults/
|
||||
# http://goldbergyoni.com/checklist-best-practice-of-node-js-in-production/
|
||||
|
||||
FROM node:16-alpine as builder
|
||||
|
||||
ENV NODE_ENV build
|
||||
|
||||
USER node
|
||||
WORKDIR /home/node
|
||||
|
||||
COPY package.json .
|
||||
COPY yarn.lock .
|
||||
|
||||
RUN yarn install --frozen-lockfile
|
||||
|
||||
COPY . /home/node
|
||||
|
||||
RUN yarn run build \
|
||||
&& yarn install --production --ignore-scripts --prefer-offline
|
||||
|
||||
# ---
|
||||
|
||||
FROM node:16-alpine
|
||||
|
||||
ENV NODE_ENV production
|
||||
|
||||
USER node
|
||||
WORKDIR /home/node
|
||||
|
||||
COPY --from=builder /home/node/package*.json /home/node/
|
||||
COPY --from=builder /home/node/node_modules/ /home/node/node_modules/
|
||||
COPY --from=builder /home/node/dist/ /home/node/dist/
|
||||
|
||||
CMD ["node", "dist/main.js"]
|
26
k8s/staging/deployment.yaml
Normal file
26
k8s/staging/deployment.yaml
Normal file
|
@ -0,0 +1,26 @@
|
|||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nestjs-boilerplate
|
||||
namespace: empatnusabangsa-staging
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nestjs-boilerplate
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nestjs-boilerplate
|
||||
spec:
|
||||
containers:
|
||||
- name: nestjs-boilerplate
|
||||
image: registry-harbor.app.bangun-kreatif.com/empatnusabangsa/nestjs-boilerplate:0.0.5
|
||||
ports:
|
||||
- containerPort: 5000
|
||||
envFrom:
|
||||
- secretRef:
|
||||
name: nestjs-boilerplate-env
|
||||
imagePullSecrets:
|
||||
- name: regcred
|
||||
|
24
k8s/staging/ingress.yaml
Normal file
24
k8s/staging/ingress.yaml
Normal file
|
@ -0,0 +1,24 @@
|
|||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
name: nestjs-boilerplate
|
||||
namespace: empatnusabangsa-staging
|
||||
annotations:
|
||||
kubernetes.io/ingress.class: "traefik"
|
||||
cert-manager.io/cluster-issuer: letsencrypt-prod
|
||||
spec:
|
||||
rules:
|
||||
- host: "nestjs-boilerplate.k3s.bangun-kreatif.com"
|
||||
http:
|
||||
paths:
|
||||
- pathType: Prefix
|
||||
path: /
|
||||
backend:
|
||||
service:
|
||||
name: nestjs-boilerplate
|
||||
port:
|
||||
number: 5000
|
||||
tls:
|
||||
- hosts:
|
||||
- "nestjs-boilerplate.k3s.bangun-kreatif.com"
|
||||
secretName: nestjs-boilerplate-k3s-bangun-kreatif-com-tls
|
4
k8s/staging/namespace.yaml
Normal file
4
k8s/staging/namespace.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: empatnusabangsa-staging
|
15
k8s/staging/secret.example.yaml
Normal file
15
k8s/staging/secret.example.yaml
Normal file
|
@ -0,0 +1,15 @@
|
|||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: nestjs-boilerplate-env
|
||||
namespace: empatnusabangsa-staging
|
||||
type: Opaque
|
||||
stringData:
|
||||
PORT: '5000'
|
||||
|
||||
DATABASE_HOST: '127.0.0.1'
|
||||
DATABASE_NAME: 'nestjs'
|
||||
DATABASE_USERNAME: 'postgres'
|
||||
DATABASE_PASSWORD: ''
|
||||
DATABASE_PORT: '5432'
|
||||
DATABASE_CLIENT: 'postgres'
|
13
k8s/staging/service.yaml
Normal file
13
k8s/staging/service.yaml
Normal file
|
@ -0,0 +1,13 @@
|
|||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: nestjs-boilerplate
|
||||
namespace: empatnusabangsa-staging
|
||||
labels:
|
||||
run: nestjs-boilerplate
|
||||
spec:
|
||||
ports:
|
||||
- port: 5000
|
||||
protocol: TCP
|
||||
selector:
|
||||
app: nestjs-boilerplate
|
|
@ -31,7 +31,9 @@
|
|||
"class-transformer": "^0.4.0",
|
||||
"class-validator": "^0.13.1",
|
||||
"joi": "^17.4.2",
|
||||
"nestjs-pino": "^2.3.1",
|
||||
"pg": "^8.7.1",
|
||||
"pino-http": "^6.3.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"rxjs": "^7.2.0",
|
||||
|
|
|
@ -4,10 +4,12 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
|
|||
import * as Joi from 'joi';
|
||||
import { UsersModule } from './users/users.module';
|
||||
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
|
||||
import { LoggerModule } from 'nestjs-pino';
|
||||
import configuration from './config/configuration';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
LoggerModule.forRoot(),
|
||||
ConfigModule.forRoot({
|
||||
load: [configuration],
|
||||
validationSchema: Joi.object({
|
||||
|
|
20
src/main.ts
20
src/main.ts
|
@ -4,21 +4,37 @@ import {
|
|||
NestFastifyApplication,
|
||||
} from '@nestjs/platform-fastify';
|
||||
import { AppModule } from './app.module';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { ConsoleLogger, LoggerService, ValidationPipe } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Logger } from 'nestjs-pino';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
AppModule,
|
||||
new FastifyAdapter(),
|
||||
{ bufferLogs: true },
|
||||
);
|
||||
|
||||
const logger = app.get(Logger);
|
||||
app.useLogger(logger);
|
||||
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
}),
|
||||
);
|
||||
|
||||
await app.listen(3000);
|
||||
const configService = app.get<ConfigService>(ConfigService);
|
||||
const port = configService.get<number>('port');
|
||||
|
||||
await app.listen(port, '0.0.0.0', (error, address) => {
|
||||
if (error) {
|
||||
logger.error(error);
|
||||
process.exit(1);
|
||||
} else {
|
||||
logger.log(`Server listening on ${address}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bootstrap();
|
||||
|
|
Loading…
Reference in New Issue
Block a user