From 97c7984dd1f569a14bfb12c0f8c1b8efc8e9fede Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Thu, 27 Jul 2023 09:33:56 +0800 Subject: [PATCH] perf: init db and config --- client/.gitignore | 3 +- client/src/api/user.ts | 4 +- client/src/hooks/useConfirm.tsx | 7 ++- client/src/pages/api/system/getInitData.ts | 3 +- .../src/pages/login/components/LoginForm.tsx | 15 +---- .../pages/login/components/RegisterForm.tsx | 2 +- client/src/service/mongo.ts | 59 ++++++++++++++++++- 7 files changed, 74 insertions(+), 19 deletions(-) diff --git a/client/.gitignore b/client/.gitignore index c040d9be1..f65f54be0 100644 --- a/client/.gitignore +++ b/client/.gitignore @@ -28,4 +28,5 @@ next-env.d.ts platform.json testApi/ local/ -.husky/ \ No newline at end of file +.husky/ +data/config.json.local \ No newline at end of file diff --git a/client/src/api/user.ts b/client/src/api/user.ts index 2fdaec3f2..55feedebe 100644 --- a/client/src/api/user.ts +++ b/client/src/api/user.ts @@ -1,6 +1,6 @@ import { GET, POST, PUT } from './request'; -import { createHashPassword, Obj2Query } from '@/utils/tools'; -import { ResLogin, PromotionRecordType } from './response/user'; +import { createHashPassword } from '@/utils/tools'; +import { ResLogin } from './response/user'; import { UserAuthTypeEnum } from '@/constants/common'; import { UserBillType, UserType, UserUpdateParams } from '@/types/user'; import type { PagingData, RequestPaging } from '@/types'; diff --git a/client/src/hooks/useConfirm.tsx b/client/src/hooks/useConfirm.tsx index c11eca95e..ed08b7014 100644 --- a/client/src/hooks/useConfirm.tsx +++ b/client/src/hooks/useConfirm.tsx @@ -32,7 +32,12 @@ export const useConfirm = (props: { title?: string; content: string }) => { ), ConfirmModal: useCallback( () => ( - + diff --git a/client/src/pages/api/system/getInitData.ts b/client/src/pages/api/system/getInitData.ts index 6fa33345e..4f08c2178 100644 --- a/client/src/pages/api/system/getInitData.ts +++ b/client/src/pages/api/system/getInitData.ts @@ -90,7 +90,8 @@ const defaultVectorModels = [ export async function getInitConfig() { try { - const res = JSON.parse(readFileSync('data/config.json', 'utf-8')); + const filename = process.env.NODE_ENV === 'development' ? 'config.json.local' : 'config.json'; + const res = JSON.parse(readFileSync(`data/${filename}`, 'utf-8')); console.log(res); global.systemEnv = res.SystemParams || defaultSystemEnv; diff --git a/client/src/pages/login/components/LoginForm.tsx b/client/src/pages/login/components/LoginForm.tsx index 29db9a666..249dd4d1e 100644 --- a/client/src/pages/login/components/LoginForm.tsx +++ b/client/src/pages/login/components/LoginForm.tsx @@ -60,15 +60,10 @@ const LoginForm = ({ setPageType, loginSuccess }: Props) => {
@@ -82,13 +77,9 @@ const LoginForm = ({ setPageType, loginSuccess }: Props) => { placeholder="密码" {...register('password', { required: '密码不能为空', - minLength: { - value: 4, - message: '密码最少4位最多12位' - }, maxLength: { value: 12, - message: '密码最少4位最多12位' + message: '密码最多12位' } })} > diff --git a/client/src/pages/login/components/RegisterForm.tsx b/client/src/pages/login/components/RegisterForm.tsx index 6e2bea73e..b23d7b13c 100644 --- a/client/src/pages/login/components/RegisterForm.tsx +++ b/client/src/pages/login/components/RegisterForm.tsx @@ -65,7 +65,7 @@ const RegisterForm = ({ setPageType, loginSuccess }: Props) => { title: `注册成功`, status: 'success' }); - // aut register a model + // auto register template app appTemplates.forEach((template) => { postCreateApp({ avatar: template.avatar, diff --git a/client/src/service/mongo.ts b/client/src/service/mongo.ts index 06e4a1498..0951923cb 100644 --- a/client/src/service/mongo.ts +++ b/client/src/service/mongo.ts @@ -2,9 +2,13 @@ import mongoose from 'mongoose'; import tunnel from 'tunnel'; import { startQueue } from './utils/tools'; import { getInitConfig } from '@/pages/api/system/getInitData'; +import { User } from './models/user'; +import { PRICE_SCALE } from '@/constants/common'; +import { connectPg, PgClient } from './pg'; +import { createHashPassword } from '@/utils/tools'; /** - * 连接 MongoDB 数据库 + * connect MongoDB and init data */ export async function connectToDatabase(): Promise { if (global.mongodb) { @@ -45,6 +49,9 @@ export async function connectToDatabase(): Promise { maxPoolSize: Number(process.env.DB_MAX_LINK || 5), minPoolSize: 2 }); + + initRootUser(); + initPg(); console.log('mongo connected'); } catch (error) { console.log('error->', 'mongo connect error'); @@ -55,6 +62,56 @@ export async function connectToDatabase(): Promise { startQueue(); } +async function initRootUser() { + try { + const rootUser = await User.findOne({ + username: 'root' + }); + if (rootUser) { + console.log('root user already exists'); + return; + } + const psw = process.env.DEFAULT_ROOT_PSW || '123456'; + await User.create({ + username: 'root', + password: createHashPassword(psw), + balance: 100 * PRICE_SCALE + }); + + console.log(`create root user success`, { + username: 'root', + password: psw + }); + } catch (error) { + console.log('init root user error', error); + initRootUser(); + } +} +async function initPg() { + try { + await connectPg(); + await PgClient.query(` + CREATE EXTENSION IF NOT EXISTS vector; + CREATE TABLE IF NOT EXISTS modelData ( + id BIGSERIAL PRIMARY KEY, + vector VECTOR(1536) NOT NULL, + user_id VARCHAR(50) NOT NULL, + kb_id VARCHAR(50) NOT NULL, + source VARCHAR(100), + q TEXT NOT NULL, + a TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS modelData_userId_index ON modelData USING HASH (user_id); + CREATE INDEX IF NOT EXISTS modelData_kbId_index ON modelData USING HASH (kb_id); + CREATE INDEX IF NOT EXISTS idx_model_data_md5_q_a_user_id_kb_id ON modelData (md5(q), md5(a), user_id, kb_id); + `); + console.log('init pg successful'); + } catch (error) { + console.log('init pg error', error); + initPg(); + } +} + export * from './models/authCode'; export * from './models/chat'; export * from './models/app';