From d7a9f68af478cba73d5415620d719af45562b16d Mon Sep 17 00:00:00 2001 From: fantasticit Date: Tue, 28 Jun 2022 21:16:53 +0800 Subject: [PATCH 1/7] client: fix seo title --- packages/client/src/pages/admin/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/pages/admin/index.tsx b/packages/client/src/pages/admin/index.tsx index 78062d37..93d98512 100644 --- a/packages/client/src/pages/admin/index.tsx +++ b/packages/client/src/pages/admin/index.tsx @@ -28,7 +28,7 @@ const Page: NextPage = () => { return ( - +
{user && user.isSystemAdmin ? ( <> From fccfaa09b7a92f1f8f579c8326b57e71810c6da7 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Tue, 28 Jun 2022 21:37:39 +0800 Subject: [PATCH 2/7] chore: update nginx.conf --- nginx.conf.sample | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/nginx.conf.sample b/nginx.conf.sample index fca91b57..f2b36de7 100644 --- a/nginx.conf.sample +++ b/nginx.conf.sample @@ -43,16 +43,23 @@ server { } location /think/wss { - proxy_pass http://think_wss; - proxy_read_timeout 300s; - proxy_send_timeout 300s; + proxy_pass http://think_wss; + proxy_read_timeout 300s; + proxy_send_timeout 300s; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection 'upgrade'; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + } + + location /static/ { + gzip_static on; + expires max; + add_header Cache-Control public; + alias /apps/think/packages/server/static/; } } From bf4389624bfba0565dbd0901589c072fc68f19e3 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Tue, 28 Jun 2022 21:16:53 +0800 Subject: [PATCH 3/7] client: fix seo title --- packages/client/src/pages/admin/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/pages/admin/index.tsx b/packages/client/src/pages/admin/index.tsx index 78062d37..93d98512 100644 --- a/packages/client/src/pages/admin/index.tsx +++ b/packages/client/src/pages/admin/index.tsx @@ -28,7 +28,7 @@ const Page: NextPage = () => { return ( - +
{user && user.isSystemAdmin ? ( <> From d7fbbb1700e6360ddfce25e1290c32ad8f7aaa4f Mon Sep 17 00:00:00 2001 From: fantasticit Date: Tue, 28 Jun 2022 21:37:39 +0800 Subject: [PATCH 4/7] chore: update nginx.conf --- nginx.conf.sample | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/nginx.conf.sample b/nginx.conf.sample index fca91b57..f2b36de7 100644 --- a/nginx.conf.sample +++ b/nginx.conf.sample @@ -43,16 +43,23 @@ server { } location /think/wss { - proxy_pass http://think_wss; - proxy_read_timeout 300s; - proxy_send_timeout 300s; + proxy_pass http://think_wss; + proxy_read_timeout 300s; + proxy_send_timeout 300s; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection 'upgrade'; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + } + + location /static/ { + gzip_static on; + expires max; + add_header Cache-Control public; + alias /apps/think/packages/server/static/; } } From bf0826b2205f1aacf8770b79af209833f25493f6 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Tue, 28 Jun 2022 23:46:57 +0800 Subject: [PATCH 5/7] chore: update docker config --- Dockerfile | 2 +- build-output.sh | 7 ++++++- config/dev.yaml | 6 +++--- config/docker-prod-sample.yaml | 6 +++--- docker/start.sh | 10 ++-------- let-us-start.md | 2 +- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 751a6b85..6ad5a105 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ COPY --from=builder /app/output/ /app/ WORKDIR /app RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories RUN npm config set registry https://registry.npmmirror.com -RUN set -x \ +RUN set -x \ && apk update \ && apk add --no-cache tzdata redis \ && chmod +x /app/docker/start.sh \ diff --git a/build-output.sh b/build-output.sh index a2caa46b..ed234320 100755 --- a/build-output.sh +++ b/build-output.sh @@ -1,7 +1,12 @@ #! /bin/bash # 该脚本只保留生产环境运行所需文件到统一目录 +if [ ! -f './config/prod.yaml' ]; then + echo "缺少 config/prod.yaml 文件,可参考 docker-prod-sample.yaml 进行配置" + exit 1 +fi # 构建 +pnpm fetch --prod pnpm install pnpm run build @@ -71,7 +76,7 @@ cd ../../ # @see https://github.com/typicode/husky/issues/914#issuecomment-826768549 cd ${outputDir} npm set-script prepare "" -pnpm install -r --prod +pnpm install -r --offline --prod cd ../ echo "${outputDir} 打包完成" diff --git a/config/dev.yaml b/config/dev.yaml index 52fb7d98..1b7e3409 100644 --- a/config/dev.yaml +++ b/config/dev.yaml @@ -29,9 +29,9 @@ server: user: '' password: '' admin: - name: 'sytemadmin' # 注意修改 - password: 'sytemadmin' # 注意修改 - email: 'sytemadmin@think.com' # 注意修改为真实邮箱地址 + name: 'admin' # 注意修改 + password: 'admin' # 注意修改 + email: 'admin@think.com' # 注意修改为真实邮箱地址 # 数据库配置 db: diff --git a/config/docker-prod-sample.yaml b/config/docker-prod-sample.yaml index b3198df4..103f5c18 100644 --- a/config/docker-prod-sample.yaml +++ b/config/docker-prod-sample.yaml @@ -29,9 +29,9 @@ server: user: '' password: '' admin: - name: 'sytemadmin' # 注意修改 - password: 'sytemadmin' # 注意修改 - email: 'sytemadmin@think.com' # 注意修改为真实邮箱地址 + name: 'admin' # 注意修改 + password: 'admin' # 注意修改 + email: 'admin@think.com' # 注意修改为真实邮箱地址 # 数据库配置 db: diff --git a/docker/start.sh b/docker/start.sh index 4a488f1c..821e6cc4 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -2,13 +2,7 @@ ### Author:jonnyan404 ### date:2022年5月22日 -CONFIG_FILE='/app/config/prod.yaml' - -if [ ! -f $CONFIG_FILE ]; then - cp -f /app/config/docker-prod-sample.yaml $CONFIG_FILE -else - echo "" -fi - pnpm run pm2 +pm2 startup +pm2 save pm2 logs diff --git a/let-us-start.md b/let-us-start.md index 17bd5f7a..1fbfbea3 100644 --- a/let-us-start.md +++ b/let-us-start.md @@ -76,7 +76,7 @@ pm2 save ### docker-compose -也可以使用 docker-compose 进行项目部署。首先,根据需要修改 `docker-compose.yml` 中的数据库、Redis 相关用户名、密码等配置,然后,修改 `config/docker-prod-sample.yaml` 中对应的配置。 +也可以使用 docker-compose 进行项目部署。首先,根据需要修改 `docker-compose.yml` 中的数据库、Redis 相关用户名、密码等配置,然后,从 `config/docker-prod-sample.yaml` 复制出 `config/prod.yaml` 并修改其中对应的配置。 ```bash # 首次安装 From fb6fdfffb50474aebc9a9037d07d9b8001bf8265 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Wed, 29 Jun 2022 00:04:22 +0800 Subject: [PATCH 6/7] feat: remove confirmPassword --- packages/client/src/helpers/validator.ts | 7 +++++++ .../src/pages/register/index.module.scss | 1 + packages/client/src/pages/register/index.tsx | 21 ++++++++++--------- packages/server/src/dtos/create-user.dto.ts | 7 +------ packages/server/src/services/user.service.ts | 4 ---- 5 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 packages/client/src/helpers/validator.ts diff --git a/packages/client/src/helpers/validator.ts b/packages/client/src/helpers/validator.ts new file mode 100644 index 00000000..126e236c --- /dev/null +++ b/packages/client/src/helpers/validator.ts @@ -0,0 +1,7 @@ +export const isEmail = (email) => { + return !!String(email) + .toLowerCase() + .match( + /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + ); +}; diff --git a/packages/client/src/pages/register/index.module.scss b/packages/client/src/pages/register/index.module.scss index 3b94d33a..4366dc25 100644 --- a/packages/client/src/pages/register/index.module.scss +++ b/packages/client/src/pages/register/index.module.scss @@ -9,6 +9,7 @@ position: relative; z-index: 10; display: flex; + height: calc(100% - 52px); padding: 10vh 24px; flex: 1; flex-direction: column; diff --git a/packages/client/src/pages/register/index.tsx b/packages/client/src/pages/register/index.tsx index cb6329f9..e414e526 100644 --- a/packages/client/src/pages/register/index.tsx +++ b/packages/client/src/pages/register/index.tsx @@ -3,11 +3,13 @@ import { Author } from 'components/author'; import { LogoImage, LogoText } from 'components/logo'; import { Seo } from 'components/seo'; import { useRegister, useVerifyCode } from 'data/user'; +import { isEmail } from 'helpers/validator'; import { useInterval } from 'hooks/use-interval'; import { useRouterQuery } from 'hooks/use-router-query'; import { useToggle } from 'hooks/use-toggle'; import Link from 'next/link'; import Router from 'next/router'; +import { emit } from 'process'; import React, { useCallback, useState } from 'react'; import styles from './index.module.scss'; @@ -25,7 +27,13 @@ const Page = () => { const { sendVerifyCode, loading: sendVerifyCodeLoading } = useVerifyCode(); const onFormChange = useCallback((formState) => { - setEmail(formState.values.email); + const email = formState.values.email; + + if (isEmail(email)) { + setEmail(email); + } else { + setEmail(null); + } }, []); const { start, stop } = useInterval(() => { @@ -89,6 +97,7 @@ const Page = () => { 用户注册 + { placeholder="输入账户名称" rules={[{ required: true, message: '请输入账户' }]} > + { placeholder="输入用户密码" rules={[{ required: true, message: '请输入密码' }]} > - Date: Wed, 29 Jun 2022 00:20:12 +0800 Subject: [PATCH 7/7] server: use shortId --- .../server/src/entities/document.entity.ts | 10 ++++- packages/server/src/entities/wiki.entity.ts | 10 ++++- .../server/src/helpers/shortid.herlper.ts | 43 +++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 packages/server/src/helpers/shortid.herlper.ts diff --git a/packages/server/src/entities/document.entity.ts b/packages/server/src/entities/document.entity.ts index 7783e3a3..b1ee38a4 100644 --- a/packages/server/src/entities/document.entity.ts +++ b/packages/server/src/entities/document.entity.ts @@ -1,10 +1,16 @@ +import { getShortId } from '@helpers/shortid.herlper'; import { DocumentStatus } from '@think/domains'; import { Exclude } from 'class-transformer'; -import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'; +import { BeforeInsert, Column, CreateDateColumn, Entity, PrimaryColumn, UpdateDateColumn } from 'typeorm'; @Entity('document') export class DocumentEntity { - @PrimaryGeneratedColumn('uuid') + @BeforeInsert() + getShortId() { + this.id = getShortId(); + } + + @PrimaryColumn() public id: string; @Column({ type: 'varchar', comment: '文档所属知识库 Id' }) diff --git a/packages/server/src/entities/wiki.entity.ts b/packages/server/src/entities/wiki.entity.ts index c3861cb6..988b1b38 100644 --- a/packages/server/src/entities/wiki.entity.ts +++ b/packages/server/src/entities/wiki.entity.ts @@ -1,10 +1,16 @@ +import { getShortId } from '@helpers/shortid.herlper'; import { DEFAULT_WIKI_AVATAR } from '@think/constants'; import { WikiStatus } from '@think/domains'; -import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'; +import { BeforeInsert, Column, CreateDateColumn, Entity, PrimaryColumn, UpdateDateColumn } from 'typeorm'; @Entity('wiki') export class WikiEntity { - @PrimaryGeneratedColumn('uuid') + @BeforeInsert() + getShortId() { + this.id = getShortId(); + } + + @PrimaryColumn() public id: string; @Column({ type: 'varchar', length: 200, comment: '知识库名称' }) diff --git a/packages/server/src/helpers/shortid.herlper.ts b/packages/server/src/helpers/shortid.herlper.ts new file mode 100644 index 00000000..043fc0e7 --- /dev/null +++ b/packages/server/src/helpers/shortid.herlper.ts @@ -0,0 +1,43 @@ +import { randomFillSync } from 'node:crypto'; + +const urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'; + +// It is best to make fewer, larger requests to the crypto module to +// avoid system call overhead. So, random numbers are generated in a +// pool. The pool is a Buffer that is larger than the initial random +// request size by this multiplier. The pool is enlarged if subsequent +// requests exceed the maximum buffer size. +const POOL_SIZE_MULTIPLIER = 128; +let pool, poolOffset; + +const fillPool = (bytes) => { + if (!pool || pool.length < bytes) { + pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER); + randomFillSync(pool); + poolOffset = 0; + } else if (poolOffset + bytes > pool.length) { + randomFillSync(pool); + poolOffset = 0; + } + poolOffset += bytes; +}; + +const nanoid = (size = 21) => { + // `-=` convert `size` to number to prevent `valueOf` abusing + fillPool((size -= 0)); + let id = ''; + // We are reading directly from the random pool to avoid creating new array + for (let i = poolOffset - size; i < poolOffset; i++) { + // It is incorrect to use bytes exceeding the alphabet size. + // The following mask reduces the random byte in the 0-255 value + // range to the 0-63 value range. Therefore, adding hacks, such + // as empty string fallback or magic numbers, is unneccessary because + // the bitmask trims bytes down to the alphabet size. + id += urlAlphabet[pool[i] & 63]; + } + return id; +}; + +export const getShortId = () => { + return nanoid(12); +};