Files
FastGPT/projects/app/src/service/mongo.ts
Archer 0f3418daf5 Publish app - feishu and wecom (#2375)
* feat(app publish): feishu bot (#2290)

* feat: feishu publish channel fe

* feat: enable feishu fe,
feat: feishu token api

* feat: feishu bot

* chore: extract saveChat from projects/app

* chore: remove debug log output

* feat: Basic Info

* chore: feishu bot fe adjusting

* feat: feishu bot docs

* feat: new tmpData collection for all tmpdata

* chore: compress the image

* perf: feishu config

* feat: source name

* perf: text desc

* perf: load system plugins

* perf: chat source

* feat(publish): Wecom bot (#2343)

* chore: Wecom Config

* feat(fe): wecom config fe

* feat: wecom fe

* chore: uses the newest editmodal

* feat: update png; adjust the fe

* chore: adjust fe

* perf: publish app ui

---------

Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com>
2024-08-13 21:52:18 +08:00

95 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PRICE_SCALE } from '@fastgpt/global/support/wallet/constants';
import { MongoUser } from '@fastgpt/service/support/user/schema';
import { connectMongo } from '@fastgpt/service/common/mongo/init';
import { hashStr } from '@fastgpt/global/common/string/tools';
import { createDefaultTeam } from '@fastgpt/service/support/user/team/controller';
import { exit } from 'process';
import { initVectorStore } from '@fastgpt/service/common/vectorStore/controller';
import { startCron } from './common/system/cron';
import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun';
import { initGlobal, getInitConfig } from './common/system';
import { startMongoWatch } from './common/system/volumnMongoWatch';
import { startTrainingQueue } from './core/dataset/training/utils';
import { systemStartCb } from '@fastgpt/service/common/system/tools';
import { addLog } from '@fastgpt/service/common/system/log';
import { getSystemPluginCb } from './core/app/plugin';
/**
* This function is equivalent to the entry to the service
* connect MongoDB and init data
*/
export function connectToDatabase() {
if (!global.systemLoadedGlobalVariables) {
global.systemLoadedGlobalVariables = true;
initGlobal();
}
return connectMongo().then(async () => {
if (global.systemLoadedGlobalConfig) return;
global.systemLoadedGlobalConfig = true;
try {
systemStartCb();
//init system configinit vector databaseinit root user
await Promise.all([getInitConfig(), getSystemPluginCb(), initVectorStore(), initRootUser()]);
startMongoWatch();
// cron
startCron();
// start queue
startTrainingQueue(true);
} catch (error) {
addLog.error('init error', error);
exit(1);
}
});
}
async function initRootUser(retry = 3): Promise<any> {
try {
const rootUser = await MongoUser.findOne({
username: 'root'
});
const psw = process.env.DEFAULT_ROOT_PSW || '123456';
let rootId = rootUser?._id || '';
await mongoSessionRun(async (session) => {
// init root user
if (rootUser) {
await rootUser.updateOne({
password: hashStr(psw)
});
} else {
const [{ _id }] = await MongoUser.create(
[
{
username: 'root',
password: hashStr(psw)
}
],
{ session }
);
rootId = _id;
}
// init root team
await createDefaultTeam({ userId: rootId, balance: 9999 * PRICE_SCALE, session });
});
console.log(`root user init:`, {
username: 'root',
password: psw
});
} catch (error) {
if (retry > 0) {
console.log('retry init root user');
return initRootUser(retry - 1);
} else {
console.error('init root user error', error);
exit(1);
}
}
}