Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
chore: release 1.9.2 (#21)
Browse files Browse the repository at this point in the history
* pref: swagger docs

* feat: Dockerfile自定义mysql主机名和端口
  • Loading branch information
buqiyuan authored Nov 14, 2022
1 parent 410bd11 commit b1ac940
Show file tree
Hide file tree
Showing 34 changed files with 309 additions and 144 deletions.
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ SWAGGER_VERSION=1.0
SWAGGER_TITLE=Eoapi-remote-server API文档
SWAGGER_DESC=Eoapi remote server API document。

# other
# eoapi web服务端口
EOAPI_WEB_SERVER_POST=3002
# http测试服务端口
EOAPI_TEST_SERVER_PORT=42011
# socket服务端口
EOAPI_WEBSOCKET_POST=42022
29 changes: 18 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,46 @@
# 使用 as 来为某一阶段命名
FROM node:lts-alpine as builder


ENV PROJECT_DIR=/eoapi-remote-server \
MYSQL_HOST=mysql \
MYSQL_PORT=3306

# WORKDIR指令用于设置Dockerfile中的RUN、CMD和ENTRYPOINT指令执行命令的工作目录(默认为/目录),该指令在Dockerfile文件中可以出现多次,
# 如果使用相对路径则为相对于WORKDIR上一次的值,
# 例如WORKDIR /data,WORKDIR logs,RUN pwd最终输出的当前目录是/data/logs。
# cd到 /eoapi-remote-server
WORKDIR /eoapi-remote-server
WORKDIR $PROJECT_DIR

# set timezone
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' > /etc/timezone

# mirror acceleration
# RUN npm config set registry https://registry.npmmirror.com
# RUN yarn config set registry https://registry.npmmirror.com
# RUN npm config rm proxy && npm config rm https-proxy

# install & build
COPY ./ /eoapi-remote-server
RUN chmod +x ./wait-for-it.sh
RUN apk update && apk add bash
# install
COPY package.json $PROJECT_DIR
RUN yarn install
RUN yarn build
# build
COPY ./ $PROJECT_DIR
RUN chmod +x ./wait-for-it.sh \
&& apk update \
&& apk add bash \
&& yarn build \
&& yarn global add pm2

# clean dev dep
# RUN rm -rf node_modules
# RUN yarn install --production
# RUN yarn cache clean

RUN yarn global add pm2

# 容器对外暴露的端口号
EXPOSE 3000

# 容器启动时执行的命令,类似npm run start
# CMD ["yarn", "start:prod"]
# CMD ["pm2-runtime", "ecosystem.config.js"]
ENTRYPOINT ./wait-for-it.sh mysql:3306 -- yarn migration:run && pm2-runtime ecosystem.config.js
ENTRYPOINT ./wait-for-it.sh $MYSQL_HOST:$MYSQL_PORT -- yarn migration:run && pm2-runtime ecosystem.config.js
11 changes: 4 additions & 7 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ services:
- 'host.docker.internal:host-gateway'
ports:
- '${EOAPI_SERVER_PORT}:3000'
# 当前服务启动之前先要把depends_on指定的服务启动起来才行
depends_on:
- mysql
networks:
- eoapi_net

Expand All @@ -29,9 +26,9 @@ services:
restart_policy:
condition: on-failure
max_attempts: 10
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci #设置utf8字符集
env_file:
- .env
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci #设置utf8字符集
ports:
- '${MYSQL_PORT}:3306'
volumes:
Expand All @@ -49,7 +46,7 @@ services:
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '3002:80'
- '${EOAPI_WEB_SERVER_POST}:80'
# volumes:
# - ./nginx.conf:/etc/nginx/conf.d/default.conf
networks:
Expand All @@ -65,8 +62,8 @@ services:
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '42011:4201'
- '42022:4202'
- '${EOAPI_TEST_SERVER_PORT}:4201'
- '${EOAPI_WEBSOCKET_POST}:4202'
networks:
- eoapi_net

Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eoapi-remote-server",
"version": "1.9.1",
"version": "1.9.2",
"description": "Storage api data in remote server",
"author": "eoapi",
"private": true,
Expand Down Expand Up @@ -32,13 +32,13 @@
},
"dependencies": {
"@nestjs/axios": "^1.0.0",
"@nestjs/common": "^9.1.6",
"@nestjs/common": "^9.2.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.1.6",
"@nestjs/core": "^9.2.0",
"@nestjs/jwt": "^9.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.1.6",
"@nestjs/platform-express": "^9.2.0",
"@nestjs/swagger": "^6.1.3",
"@nestjs/typeorm": "^9.0.1",
"ajv": "^8.11.0",
Expand All @@ -60,22 +60,22 @@
"devDependencies": {
"@nestjs/cli": "^9.1.5",
"@nestjs/schematics": "^9.0.3",
"@nestjs/testing": "^9.1.6",
"@nestjs/testing": "^9.2.0",
"@types/crypto-js": "^4.1.1",
"@types/express": "^4.17.14",
"@types/jest": "29.2.1",
"@types/jest": "29.2.2",
"@types/node": "^18.11.9",
"@types/passport": "^1.0.11",
"@types/passport-http": "^0.3.9",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"dotenv": "^16.0.3",
"eslint": "^8.26.0",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "29.2.2",
"jest": "29.3.1",
"prettier": "^2.7.1",
"source-map-support": "^0.5.21",
"supertest": "^6.3.1",
Expand Down
98 changes: 84 additions & 14 deletions src/common/class/res.class.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,96 @@
export class ResOp {
readonly data: any;
readonly statusCode: number;
readonly message: string;
import { applyDecorators, Type } from '@nestjs/common';
import {
ApiExtraModels,
ApiProperty,
ApiResponse,
ApiResponseSchemaHost,
getSchemaPath,
} from '@nestjs/swagger';

constructor(code: number, data?: any, message = 'success') {
this.statusCode = code;
class EmptyClass {}

export class ResponseDto<T> {
data: T;

@ApiProperty({ default: 200 })
statusCode: number;

@ApiProperty({ default: 'success' })
message: string;

constructor(statusCode: number, data?: any, message = 'success') {
this.statusCode = statusCode;
this.data = data;
this.message = message;
}

static success(data?: any) {
return new ResOp(200, data);
return new ResponseDto(200, data);
}
}

export class Pagination {
export class PaginatedDto<TData> {
@ApiProperty()
total: number;
page: number;
size: number;
}

export class PageResult<T> {
list?: Array<T>;
pagination: Pagination;
@ApiProperty()
limit: number;

@ApiProperty()
offset: number;

results: TData[];
}

export const CustomApiResponse = <
DataDto extends Type<any>,
WrapperDataDto extends Type<unknown>,
>(
dataDto: DataDto,
wrapperDataDto: WrapperDataDto,
dataType = 'object',
options?: Partial<ApiResponseSchemaHost>,
) =>
applyDecorators(
ApiExtraModels(wrapperDataDto, dataDto),
ApiResponse({
status: 200,
schema: {
allOf: [
{ $ref: getSchemaPath(wrapperDataDto) },
{
properties: {
data: {
type: dataType,
[dataType === 'object' ? '$ref' : 'items']:
dataType === 'object'
? getSchemaPath(dataDto)
: { $ref: getSchemaPath(dataDto) },
},
},
},
],
},
...options,
}),
);

export const ApiOkResponseData = <DataDto extends Type<any>>(
dataDto?: DataDto,
dataType: 'object' | 'array' = 'object',
options?: Partial<ApiResponseSchemaHost>,
) => CustomApiResponse(dataDto ?? EmptyClass, ResponseDto, dataType, options);

export const ApiCreatedResponseData = <DataDto extends Type<any>>(
dataDto?: DataDto,
dataType: 'object' | 'array' = 'object',
options?: Partial<ApiResponseSchemaHost>,
) =>
CustomApiResponse(dataDto ?? EmptyClass, ResponseDto, dataType, {
status: 201,
...options,
});

export const ApiOkResponsePaginated = <DataDto extends Type<any>>(
dataDto: DataDto,
) => CustomApiResponse(dataDto, PaginatedDto);
4 changes: 2 additions & 2 deletions src/common/filters/api-exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
HttpStatus,
} from '@nestjs/common';
import { ApiException } from '../exceptions/api.exception';
import { ResOp } from '../class/res.class';
import { ResponseDto } from '../class/res.class';
import { isDev } from '@/utils';

const errorTips = {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class ApiExceptionFilter implements ExceptionFilter {
message =
exception instanceof HttpException ? exception.message : `${exception}`;
// }
const result = new ResOp(code, null, message);
const result = new ResponseDto(code, null, message);
response.status(status).send(result);
}
}
4 changes: 2 additions & 2 deletions src/common/interceptors/api-transform.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Reflector } from '@nestjs/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { TRANSFORM_KEEP_KEY_METADATA } from '../contants/decorator.contants';
import { ResOp } from '../class/res.class';
import { ResponseDto } from '../class/res.class';

/**
* 统一处理返回接口结果,如果不需要则添加@Keep装饰器
Expand All @@ -25,7 +25,7 @@ export class ApiTransformInterceptor implements NestInterceptor {
} else {
const response = context.switchToHttp().getResponse();
response.header('Content-Type', 'application/json; charset=utf-8');
return new ResOp(200, data);
return new ResponseDto(200, data);
}
}),
);
Expand Down
13 changes: 3 additions & 10 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { DataSource, DataSourceOptions } from 'typeorm';

export let appDataSource: DataSource;

export const getAppDataSource = (): DataSource => {
return appDataSource;
};
import { DataSourceOptions } from 'typeorm';

export const getConfiguration = () => {
const config = {
Expand All @@ -18,7 +12,7 @@ export const getConfiguration = () => {
host: process.env.MYSQL_HOST,
port: Number.parseInt(process.env.MYSQL_PORT, 10),
username: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_ROOT_PASSWORD,
password: process.env.MYSQL_PASSWORD || process.env.MYSQL_ROOT_PASSWORD,
database: process.env.MYSQL_DATABASE,
entities: [__dirname + '/../**/entities/*.entity.{ts,js}'],
autoLoadEntities: true,
Expand All @@ -40,8 +34,7 @@ export const getConfiguration = () => {
version: process.env.SWAGGER_VERSION,
},
};
appDataSource = new DataSource(config.database);
// appDataSource.initialize();

return config;
};

Expand Down
3 changes: 3 additions & 0 deletions src/entities/apiData.entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiHideProperty } from '@nestjs/swagger';
import { Column, Entity, Generated, ManyToOne, OneToMany } from 'typeorm';
import { Exclude } from 'class-transformer';
import { Base } from './base.entity';
Expand Down Expand Up @@ -58,12 +59,14 @@ export class ApiData extends Base {
@Column({ default: 0 })
weight: number;

@ApiHideProperty()
@Exclude()
@ManyToOne(() => Project, (project) => project.apiData, {
onDelete: 'CASCADE',
})
project: Project;

@ApiHideProperty()
@Exclude()
@OneToMany(() => Mock, (mock) => mock.apiData)
mock: Mock[];
Expand Down
2 changes: 2 additions & 0 deletions src/entities/apiGroup.entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiHideProperty } from '@nestjs/swagger';
import { Column, Entity, ManyToOne } from 'typeorm';
import { Exclude } from 'class-transformer';
import { Project } from './project.entity';
Expand All @@ -14,6 +15,7 @@ export class ApiGroup extends Base {
@Column({ default: 0 })
weight: number;

@ApiHideProperty()
@Exclude()
@ManyToOne(() => Project, (project) => project.apiGroup, {
onDelete: 'CASCADE',
Expand Down
2 changes: 2 additions & 0 deletions src/entities/apiTestHistory.entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiHideProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import { Column, Entity, ManyToOne } from 'typeorm';
import { Project } from './project.entity';
Expand All @@ -20,6 +21,7 @@ export class ApiTestHistory extends OperatorBase {
@Column({ type: 'json' })
response: string;

@ApiHideProperty()
@Exclude()
@ManyToOne(() => Project, (project) => project.apiTestHistory, {
onDelete: 'CASCADE',
Expand Down
Loading

0 comments on commit b1ac940

Please sign in to comment.