Skip to content

Commit

Permalink
feat: upgrade to new version
Browse files Browse the repository at this point in the history
  • Loading branch information
cyber-eternal committed Nov 6, 2022
1 parent f2f255b commit 5927231
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 30 deletions.
5 changes: 2 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ NODE_ENV=
APP_PORT=
SERVICE_PATH=
SWAGGER_HOST=
SWAGGER_USER_NAME=
SWAGGER_PASSWORD=

#AWS
AWS_ACCESS_KEY_ID=
Expand All @@ -29,9 +31,6 @@ TYPEORM_LOGGING=
TYPEORM_AUTO_LOAD_ENTITIES=
TYPEORM_MIGRATIONS_RUN=
TYPEORM_SYNCHRONIZE=
TYPEORM_SYNCHRONIZE=
TYPEORM_MIGRATIONS_RUN=
TYPEORM_LOGGING=

# Redis
REDIS_HOST=
Expand Down
2 changes: 1 addition & 1 deletion config/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
nodeEnv: process.env.NODE_ENV || 'dev',
nodeEnv: process.env.NODE_ENV || 'local',
port: process.env.APP_PORT || 3000,
version: process.env.npm_package_version || '1.0',
};
4 changes: 3 additions & 1 deletion config/schema/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as Joi from 'joi';

export default {
NODE_ENV: Joi.string().valid('dev', 'prod', 'test', 'stage').default('dev'),
NODE_ENV: Joi.string()
.valid('dev', 'prod', 'local', 'test', 'stage')
.default('dev'),
PORT: Joi.number().default(3000),
};
3 changes: 2 additions & 1 deletion config/swagger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default {
swaggerPath: process.env.SWAGGER_PATH || '/api/doc',
swaggerHost: process.env.SWAGGER_HOST || 'http://localhost:3000',
oAuth2RedirectFilePath: process.env.OAUTH2_REDIRECT_FILE_PATH || '',
swaggerUsername: process.env.SWAGGER_USER_NAME,
swaggerPassword: process.env.SWAGGER_PASSWORD,
};
File renamed without changes.
5 changes: 1 addition & 4 deletions nodemon-debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
"src/**/*.spec.ts"
],
"exec": "node --inspect=0.0.0.0:9229 -r ts-node/register -r tsconfig-paths/register src/main.ts",
"env": {
"NODE_ENV": "development",
"APP_PORT": 3001
}
"env": {}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"express": "^4.18.2",
"express-basic-auth": "^1.2.1",
"express-rate-limit": "^6.6.0",
"graphql": "^16.6.0",
"helmet": "^6.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
NestModule,
RequestMethod,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { BootstrapConfigModule } from '@app/bootstrap/config.module';
import { AuthUserMiddleware } from '@app/common/middlewares/auth-user.middleware';
import { BootstrapTypeormModule } from '@app/bootstrap/typeorm.module';
import { ConfigService } from '@nestjs/config';
import { PingModule } from '@app/modules/ping/ping.module';
// import { AwsModule } from './modules/aws/aws.module';
// import { BootstrapRedisModule } from '@app/bootstrap/redis.module';
import { PingModule } from '@app/modules/ping/ping.module';
// import { AwsModule } from '@app/modules/aws/aws.module';
// import { BootstrapGraphqlModule } from '@app/bootstrap/graphql.module';

@Module({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { INestApplication } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Request, Response, NextFunction } from 'express';
import * as basicAuth from 'express-basic-auth';

export const setupSwagger = (app: INestApplication) => {
const configService = app.get(ConfigService);

if (configService.get<string>('NODE_ENV') === 'development') {
if (configService.get<string>('NODE_ENV') !== 'prod') {
const swaggerPath = configService.get<string>('swagger.swaggerPath');
const swaggerHost = configService.get<string>('swagger.swaggerHost');
const version = configService.get<string>('app.version');
const swaggerUsername = configService.get<string>(
'swagger.swaggerUsername',
);
const swaggerPassword = configService.get<string>(
'swagger.swaggerPassword',
);

app.use(swaggerPath, (req: Request, res: Response, next: NextFunction) => {
// A temporary solution to prevent security issues with query params
Expand All @@ -21,6 +28,20 @@ export const setupSwagger = (app: INestApplication) => {
}
});

const swaggerAuth = (username: string, password: string): boolean => {
const userMatches = basicAuth.safeCompare(username, swaggerUsername);
const passwordMatches = basicAuth.safeCompare(password, swaggerPassword);
return userMatches && passwordMatches;
};

app.use(
swaggerPath,
basicAuth({
authorizer: swaggerAuth.bind({ swaggerUsername, swaggerPassword }),
challenge: true,
}),
);

const document = SwaggerModule.createDocument(
app,
new DocumentBuilder()
Expand Down
10 changes: 5 additions & 5 deletions src/common/lib/amqp/amqp.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ERROR_QUEUE_KEY = 'error';
export abstract class AMQPController {
private connection: Amqp.Connection;
private channel: Amqp.Channel;
private queueListener;
private queueListener: any;

protected constructor(
// protected readonly emailService: EmailService,
Expand All @@ -47,7 +47,7 @@ export abstract class AMQPController {
[UPDATE]: this.handleUpdate.bind(this),
[DELETE]: this.handleDraft.bind(this),
};
this.queueListener.subscribe(async (message) => {
this.queueListener.subscribe(async (message: any) => {
const content = safeJsonParse(message.content.toString());
const { type, payload } = content || {
type: undefined,
Expand Down Expand Up @@ -134,15 +134,15 @@ export abstract class AMQPController {
}
}

protected async handlePublish(payload) {
protected async handlePublish(payload: any) {
throw new Error('Not Implemented');
}

protected async handleDraft(payload) {
protected async handleDraft(payload: any) {
throw new Error('Not Implemented');
}

protected async handleUpdate(payload) {
protected async handleUpdate(payload: any) {
throw new Error('Not Implemented');
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/json/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const safeJsonParse = (val) => {
export const safeJsonParse = (val: string) => {
try {
return JSON.parse(val);
} catch (e) {
Expand Down
4 changes: 0 additions & 4 deletions src/common/utils/promise/index.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from '@app/app.module';
import { setupSwagger } from '@app/bootstrap/setup-swagger';
import { ConfigService } from '@nestjs/config';
import { setupSwagger } from '@app/bootstrap/swagger.module';
import setBaseMiddlewares from '@app/common/middlewares/base.middlewares';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import 'reflect-metadata';

(async () => {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);

setBaseMiddlewares(app);
app.setGlobalPrefix('api/v1');

setBaseMiddlewares(app);
setupSwagger(app);

await app.listen(configService.get<number>('app.port'));
Expand Down
1 change: 0 additions & 1 deletion src/modules/ping/ping.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class PingController {
@ApiOkResponse({ status: HttpStatus.OK, type: PingDto })
public ping(): PingDto {
const dto = new PingDto(this.configService.get<string>('app.version'));
console.log('PingDto: ', dto);
return dto;
}
}
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,13 @@ base64-js@^1.0.2, base64-js@^1.3.1:
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

basic-auth@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-2.0.1.tgz#b998279bf47ce38344b4f3cf916d4679bbf51e3a"
integrity sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==
dependencies:
safe-buffer "5.1.2"

[email protected]:
version "9.0.0"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.0.0.tgz#805880f84a329b5eac6e7cb6f8274b6d82bdf075"
Expand Down Expand Up @@ -3317,6 +3324,13 @@ expect@^29.0.0, expect@^29.2.1:
jest-message-util "^29.2.1"
jest-util "^29.2.1"

express-basic-auth@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/express-basic-auth/-/express-basic-auth-1.2.1.tgz#d31241c03a915dd55db7e5285573049cfcc36381"
integrity sha512-L6YQ1wQ/mNjVLAmK3AG1RK6VkokA1BIY6wmiH304Xtt/cLTps40EusZsU1Uop+v9lTDPxdtzbFmdXfFO3KEnwA==
dependencies:
basic-auth "^2.0.1"

express-rate-limit@^6.6.0:
version "6.6.0"
resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-6.6.0.tgz#3bbc2546540d327b1b0bfa9ab5f1b2c49075af98"
Expand Down

0 comments on commit 5927231

Please sign in to comment.