From cdab03190c8d412921458043fafce734e812276d Mon Sep 17 00:00:00 2001 From: wuhan <18852676227@163.com> Date: Wed, 31 Jan 2024 17:10:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E7=BD=B2=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 31 +++++++++++++++++++++++++++++++ docker-compose.yml | 26 ++++++++++++++++++++++++++ prod.yaml | 8 ++++++++ src/app.module.ts | 6 +++++- src/client/client.service.ts | 8 ++++++++ src/main.ts | 2 +- src/utils/config.ts | 29 +++++++++++++++-------------- 7 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 prod.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a197644 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +FROM node:18-alpine + +# 设置时区 +ENV TZ=Asia/Shanghai +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +# 指定工作目录 +WORKDIR /app + +# 复制当前代码到/app工作目录 +COPY . . + +# npm 源,选用国内镜像源以提高下载速度 +RUN npm config set registry https://registry.npm.taobao.org/ + +# 使用通配符来确保 package.json 和 package-lock.json 被复制 +COPY package.json /app/package.json + +# 安装应用依赖 +RUN cd /app && rm -rf /app/node_modules && node -v && npm cache verify && npm config set strict-ssl false && npm install --force + + + +# 打包 +RUN npm run build + +# 启动服务 +# "start:prod": "cross-env NODE_ENV=production node ./dist/src/main.js", +CMD npm run start:prod + +EXPOSE 7666 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ea7c557 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: "1.0" + +services: + drive-server: + # nestjs服务 + container_name: drive-server + build: + # 根据Dockerfile构建镜像 + context: . + dockerfile: Dockerfile + ports: + - 7666:7666 + restart: on-failure # 设置自动重启,这一步必须设置,主要是存在mysql还没有启动完成就启动了node服务 + volumes: + - ./prod.yaml:/prod:yaml + environment: + - HOST = 192.168.10.13 + - PASSWORD = '123456' + # networks: + # - drive-server + + # 声明一下网桥 my-server。 + # 重要:将所有服务都挂载在同一网桥即可通过容器名来互相通信了 + # 如nestjs连接mysql和redis,可以通过容器名来互相通信 + # networks: + # drive-server: diff --git a/prod.yaml b/prod.yaml new file mode 100644 index 0000000..7f7e064 --- /dev/null +++ b/prod.yaml @@ -0,0 +1,8 @@ +TEST_VALUE: + name: cookie +# 数据库相关 +DATABASE: drive +USER: root +PORT: 3306 +HOST: 192.168.10.13 +PASSWORD: '123456' \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index cb640a9..6a07efa 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -12,7 +12,11 @@ import { getMysql } from './utils/config'; // 加载连接数据库 TypeOrmModule.forRootAsync({ useFactory: async () => { - return getMysql() as TypeOrmModuleAsyncOptions; + return { + ...getMysql(), entities: [__dirname + '/**/*.entity{.ts,.js}'], // 扫描本项目中.entity.ts或者.entity.js的文件 + synchronize: false, + logging: true, + } as TypeOrmModuleAsyncOptions; }, }), ClientModule, diff --git a/src/client/client.service.ts b/src/client/client.service.ts index 87a5240..cdb0b75 100644 --- a/src/client/client.service.ts +++ b/src/client/client.service.ts @@ -16,6 +16,7 @@ export class ClientService { * 获取家庭统计信息 */ async queryHccNum({ areaCode }) { + console.log('queryHccNum'); const [parent, child] = await Promise.all([ clientMapper.queryHccNum({ areaCode }), clientMapper.queryHccChildNum({ areaCode }), @@ -51,6 +52,7 @@ export class ClientService { * @returns */ async queryUserNum({ areaCode }) { + console.log('queryUserNum'); const [parent, child, childNum] = await Promise.all([ await clientMapper.queryHccNum({ areaCode }), await clientMapper.queryUserNum({ areaCode }), @@ -94,6 +96,7 @@ export class ClientService { * 4.4获取热门应用服务信息 */ async queryMpNum({ areaCode, topLimit = 6, type = 0 }) { + console.log('queryMpNum'); const { startTime, endTime } = this.dealTime(type); const count = await clientMapper.queryMp({ areaCode, @@ -248,6 +251,7 @@ export class ClientService { * 4.5获取用户档案列表 */ async queryArchiveList({ areaCode, pageNo = 0, pageSize = 10, type }) { + console.log('queryArchiveList'); // await clientMapper.queryArchiveListNum(areaCode, type), const [info, count] = await clientMapper.queryArchiveList({ areaCode, @@ -274,6 +278,7 @@ export class ClientService { * 4.6获取用户健康档案数据 */ async queryDataList({ archiveId, hccId, areaCode }) { + console.log('queryDataList'); const dataList = await clientMapper.queryDataList({ archiveId, hccId, @@ -319,6 +324,7 @@ export class ClientService { * 4.7告警数据统计 */ async queryWarnData({ areaCode, timeRange }) { + console.log('queryWarnData'); const tokenResult = await axios.post( `http://106.14.155.39:60036/api/v1/szjt/getToken`, { @@ -349,6 +355,7 @@ export class ClientService { * 4.8获取告警数据列表 */ async queryWarnList({ code, type, time, page }) { + console.log('queryWarnList'); // let pageSize, pageIndex; // if (!page) { // pageSize = 10; @@ -464,6 +471,7 @@ export class ClientService { * 4.9获取平台家庭数据使用记录 */ async queryDataUseNum({ areaCode, dateRange = 1, limit = 7 }) { + console.log('queryDataUseNum'); const { startTime, endTime } = this.dealTime(dateRange); const result = await clientMapper.queryDataUseNum({ areaCode, diff --git a/src/main.ts b/src/main.ts index e79937d..d1a6610 100644 --- a/src/main.ts +++ b/src/main.ts @@ -36,6 +36,6 @@ async function bootstrap() { app.useGlobalPipes(new ValidationPipe()); app.useStaticAssets(join(__dirname, '..', 'public')); - await app.listen(60036, '0.0.0.0'); + await app.listen(7666, '0.0.0.0'); } bootstrap(); diff --git a/src/utils/config.ts b/src/utils/config.ts index e6ba3e8..d5a7723 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -18,20 +18,21 @@ export const getConfig = () => { return configAll } const environment = getEnv(); - const yamlPath = path.join(process.cwd(), `./.config/.${environment}.yaml`); + // const yamlPath = path.join(process.cwd(), `./.config/.${environment}.yaml`); + const yamlPath = path.join(process.cwd(), `./${environment}.yaml`); const file = fs.readFileSync(yamlPath, 'utf8'); let config = parse(file); - if (process?.argv[2]?.length) { - config = { ...config, HOST: process.argv[2] } - } - if (process?.argv[3]?.length) { - config = { ...config, USER: process.argv[3] } - } - if (process?.argv[4]?.length) { - config = { ...config, PASSWORD: process.argv[4] } - } + // if (process?.argv[2]?.length) { + // config = { ...config, HOST: process.argv[2] } + // } + // if (process?.argv[3]?.length) { + // config = { ...config, USER: process.argv[3] } + // } + // if (process?.argv[4]?.length) { + // config = { ...config, PASSWORD: process.argv[4] } + // } // 融合注入参数 - // config = Object.assign(config, process.env); + config = Object.assign(config, process.env); console.log(config) configAll = { ...config, isExist: true } return config; @@ -51,9 +52,9 @@ export const getMysql = () => { // username: 'root', // password: '123456', // database: 'drive', - entities: [__dirname + '/**/*.entity{.ts,.js}'], // 扫描本项目中.entity.ts或者.entity.js的文件 - synchronize: false, - logging: true, + // entities: [__dirname + '/**/*.entity{.ts,.js}'], // 扫描本项目中.entity.ts或者.entity.js的文件 + // synchronize: false, + // logging: true, } console.log(mysql);