feat: 服务端配置优化 (#4)

This commit is contained in:
luch 2023-11-14 22:57:52 +08:00 committed by GitHub
parent de2deeb49c
commit 0fda6cdc0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 100 additions and 236 deletions

View File

@ -1,39 +1,7 @@
import { Collection, MongoClient, ObjectId } from 'mongodb'
import { getConfig } from '../config/index'
import { getMongoMemoryUri } from '../../../getMongoMemoryUri'
import MongoService from '../../../utils/mongoService'
const config = getConfig()
class mongoService {
isInit: boolean
client: MongoClient
constructor() {
this.client = new MongoClient(config.mongo.url);
}
async getCollection({ collectionName }): Promise<Collection> {
if (process.env.SERVER_ENV === 'local') {
const uri = await getMongoMemoryUri()
this.client = new MongoClient(uri);
}
await this.client.connect()
return this.client.db(config.mongo.dbName).collection(collectionName)
}
convertId2StringByDoc(doc: any): any {
doc._id = doc._id.toString()
return doc;
}
convertId2StringByList(list: Array<any>): Array<any> {
return list.map(e => {
return this.convertId2StringByDoc(e);
})
}
getObjectIdByStr(str: string): ObjectId {
return new ObjectId(str)
}
}
export const mongo = new mongoService()
export const mongo = new MongoService({ url: config.mongo.url, dbName: config.mongo.dbName })

View File

@ -1,10 +1,10 @@
import { mongo } from '../db/mongo'
import { DICT_TYPE } from '../types/index'
import { DICT_TYPE } from '../../../types'
import { participle } from '../utils/index'
class SecurityService {
async isHitKeys({ content, dictType }: { content: string, dictType: DICT_TYPE }) {
const securityDictModel = await mongo.getCollection({ collectionName: 'securityDict' });
const securityDictModel = await mongo.getCollection({ collectionName: 'securityDict' })
const keywordList = participle({ content })
const hitCount = await securityDictModel.countDocuments({ keyword: { $in: keywordList }, type: dictType })
return hitCount > 0

View File

@ -1,14 +0,0 @@
export enum DICT_TYPE {
danger = "danger",
secret = "secret",
}
export class CommonError extends Error {
code: number
errmsg: number
constructor(msg, code = 500) {
super(msg)
this.errmsg = msg;
this.code = code;
}
}

View File

@ -1,4 +1,3 @@
import { CommonError } from '../types/index'
export function participle({ content, minLen, maxLen }: { content: string, minLen?: number, maxLen?: number }) {
const keys: Array<string> = []

View File

@ -1,7 +1,7 @@
const config = {
mongo: {
url: process.env.xiaojuSurveyMongoUrl || 'mongodb://localhost:27017',
dbName: 'surveyEengine'
dbName: 'xiaojuSurvey',
}
}

View File

@ -1,39 +1,6 @@
import { Collection, MongoClient, ObjectId } from 'mongodb'
import { getConfig } from '../config/index'
import { getMongoMemoryUri } from '../../../getMongoMemoryUri'
import MongoService from '../../../utils/mongoService'
const config = getConfig()
class mongoService {
isInit: boolean
client: MongoClient
constructor() {
this.client = new MongoClient(config.mongo.url);
}
async getCollection({ collectionName }): Promise<Collection> {
if (process.env.SERVER_ENV === 'local') {
const uri = await getMongoMemoryUri()
this.client = new MongoClient(uri);
}
await this.client.connect()
return this.client.db(config.mongo.dbName).collection(collectionName)
}
convertId2StringByDoc(doc: any): any {
doc._id = doc._id.toString()
return doc;
}
convertId2StringByList(list: Array<any>): Array<any> {
return list.map(e => {
return this.convertId2StringByDoc(e);
})
}
getObjectIdByStr(str: string): ObjectId {
return new ObjectId(str)
}
}
export const mongo = new mongoService()
export const mongo = new MongoService({ url: config.mongo.url, dbName: config.mongo.dbName })

View File

@ -3,7 +3,7 @@ import { Request, Response } from 'koa'
import { surveyService } from './service/surveyService'
import { userService } from './service/userService'
import { surveyHistoryService } from './service/surveyHistoryService'
import { HISTORY_TYPE } from './types/index'
import { HISTORY_TYPE } from '../../types/index'
import { getValidateValue } from './utils/index'
import * as Joi from 'joi'

View File

@ -1,6 +1,6 @@
import { mongo } from '../db/mongo'
import { getStatusObject } from '../utils/index'
import { SURVEY_STATUS, HISTORY_TYPE, UserType } from '../types/index'
import { SURVEY_STATUS, HISTORY_TYPE, UserType } from '../../../types/index'
class SurveyHistoryService {
async addHistory(surveyData: { surveyId: string, configData: any, type: HISTORY_TYPE, userData: UserType }) {

View File

@ -1,6 +1,6 @@
import { mongo } from '../db/mongo'
import { rpcInvote } from '../../../rpc'
import { SURVEY_STATUS, QUESTION_TYPE, CommonError, UserType, DICT_TYPE } from '../types/index'
import { SURVEY_STATUS, QUESTION_TYPE, CommonError, UserType, DICT_TYPE } from '../../../types/index'
import { getStatusObject, genSurveyPath, getMapByKey, hanleSensitiveDate } from '../utils/index'
import * as path from "path";
import * as _ from "lodash";

View File

@ -1,6 +1,6 @@
import { rpcInvote } from '../../../rpc'
import { Request } from 'koa'
import { UserType, CommonError } from '../types/index'
import { UserType, CommonError } from '../../../types/index'
class UserService {
async checkLogin({ req }: { req: Request }) {

View File

@ -1,5 +1,4 @@
import { SURVEY_STATUS } from '../types/index'
import { CommonError } from '../types/index'
import { SURVEY_STATUS, CommonError } from '../../../types/index'
import { hex2Base58 } from './base58'
import * as Joi from 'joi'

View File

@ -1,7 +1,7 @@
const config = {
mongo:{
url: process.env.xiaojuSurveyMongoUrl ||'mongodb://localhost:27017',
dbName:'surveyEengine'
dbName:'xiaojuSurvey',
}
}

View File

@ -1,39 +1,6 @@
import { Collection, MongoClient, ObjectId } from 'mongodb'
import { getConfig } from '../config/index'
import { getMongoMemoryUri } from '../../../getMongoMemoryUri'
import MongoService from '../../../utils/mongoService'
const config = getConfig()
class mongoService {
isInit:boolean
client:MongoClient
constructor() {
this.client = new MongoClient(config.mongo.url);
}
async getCollection({ collectionName }): Promise<Collection> {
if (process.env.SERVER_ENV === 'local') {
const uri = await getMongoMemoryUri()
this.client = new MongoClient(uri);
}
await this.client.connect()
return this.client.db(config.mongo.dbName).collection(collectionName)
}
convertId2StringByDoc(doc:any):any {
doc._id = doc._id.toString()
return doc;
}
convertId2StringByList(list:Array<any>):Array<any> {
return list.map(e=>{
return this.convertId2StringByDoc(e);
})
}
getObjectIdByStr(str:string) {
return new ObjectId(str)
}
}
export const mongo = new mongoService()
export const mongo = new MongoService({ url: config.mongo.url, dbName: config.mongo.dbName })

View File

@ -1,6 +1,6 @@
import { mongo } from '../db/mongo'
import { surveyKeyStoreService } from './surveyKeyStoreService'
import {CommonError} from '../types/index'
import {CommonError} from '../../../types/index'
class SurveyPublishService {
async get({surveyPath}:{surveyPath:string}) {

View File

@ -1,6 +1,6 @@
import { mongo } from '../db/mongo'
import { getStatusObject, getMapByKey } from '../utils/index'
import { SURVEY_STATUS, CommonError } from '../types/index'
import { SURVEY_STATUS, CommonError } from '../../../types/index'
import { surveyKeyStoreService } from './surveyKeyStoreService'
import * as moment from 'moment'

View File

@ -1,17 +0,0 @@
export enum SURVEY_STATUS {
new = "new",
editing = "editing",
pausing = "pausing",
published = "published",
removed = "removed",
}
export class CommonError extends Error {
code:number
errmsg:string
constructor(msg,code=500) {
super(msg)
this.code = code;
this.errmsg = msg;
}
}

View File

@ -1,7 +1,7 @@
import {
createHash
} from 'crypto';
import { CommonError } from '../types/index'
import { CommonError } from '../../../types/index'
const hash256 = (text) => {
return createHash('sha256').update(text).digest('hex')

View File

@ -1,5 +1,4 @@
import { SURVEY_STATUS } from '../types/index'
import { CommonError } from '../types/index'
import { SURVEY_STATUS, CommonError } from '../../../types/index'
import * as Joi from 'joi'
export function getMapByKey({data,key}:{data:Array<any>,key:string}) {

View File

@ -1,7 +1,7 @@
const config = {
mongo:{
url: process.env.xiaojuSurveyMongoUrl ||'mongodb://localhost:27017',
dbName:'surveyEengine'
dbName:'xiaojuSurvey',
},
jwt:{
secret: process.env.xiaojuSurveyJwtSecret || 'xiaojuSurveyJwtSecret',

View File

@ -1,39 +1,6 @@
import { Collection, MongoClient, ObjectId } from 'mongodb'
import { getConfig } from '../config/index'
import { getMongoMemoryUri } from '../../../getMongoMemoryUri'
import MongoService from '../../../utils/mongoService'
const config = getConfig()
class mongoService {
isInit: boolean
client: MongoClient
constructor() {
this.client = new MongoClient(config.mongo.url);
}
async getCollection({ collectionName }): Promise<Collection> {
if (process.env.SERVER_ENV === 'local') {
const uri = await getMongoMemoryUri()
this.client = new MongoClient(uri);
}
await this.client.connect()
return this.client.db(config.mongo.dbName).collection(collectionName)
}
convertId2StringByDoc(doc: any): any {
doc._id = doc._id.toString()
return doc;
}
convertId2StringByList(list: Array<any>): Array<any> {
return list.map(e => {
return this.convertId2StringByDoc(e);
})
}
getObjectIdByStr(str: string) {
return new ObjectId(str)
}
}
export const mongo = new mongoService()
export const mongo = new MongoService({ url: config.mongo.url, dbName: config.mongo.dbName })

View File

@ -7,7 +7,7 @@ import {
} from 'crypto';
import { mongo } from '../db/mongo'
import { getStatusObject } from '../utils/index'
import { SURVEY_STATUS, CommonError } from '../types/index'
import { SURVEY_STATUS, CommonError } from '../../../types/index'
import { getConfig } from '../config/index'
const config = getConfig()

View File

@ -1,30 +0,0 @@
export enum SURVEY_STATUS {
new = "new",
editing = "editing",
pausing = "pausing",
published = "published",
removed = "removed",
}
export enum QUESTION_TYPE {
enps = "enps",
nps = "nps",
question = "question", //通用问卷
register = "register", //报名
vote = "vote", //投票
}
export enum HISTORY_TYPE {
dailyHis = "dailyHis", //保存历史
publishHis = "publishHis", //发布历史
}
export class CommonError extends Error {
code:number
errmsg:string
constructor(msg,code=500) {
super(msg)
this.code = code;
this.errmsg = msg;
}
}

View File

@ -1,5 +1,4 @@
import { SURVEY_STATUS } from '../types/index'
import { CommonError } from '../types/index'
import { SURVEY_STATUS, CommonError } from '../../../types/index'
import * as Joi from 'joi'
export function getStatusObject({status}:{status:SURVEY_STATUS}) {
return {

View File

@ -1,6 +1,6 @@
export enum DICT_TYPE {
danger = "danger",
secret = "secret",
secret = "secret"
}
export enum SURVEY_STATUS {
@ -8,7 +8,7 @@ export enum SURVEY_STATUS {
editing = "editing",
pausing = "pausing",
published = "published",
removed = "removed",
removed = "removed"
}
export enum QUESTION_TYPE {
@ -16,28 +16,28 @@ export enum QUESTION_TYPE {
nps = "nps",
question = "question", //通用问卷
register = "register", //报名
vote = "vote", //投票
vote = "vote" //投票
}
export enum HISTORY_TYPE {
dailyHis = "dailyHis", //保存历史
publishHis = "publishHis", //发布历史
publishHis = "publishHis" //发布历史
}
export interface UserType {
_id:string,
username:string,
password:string,
curStatus:any,
createDate:number
_id: string;
username: string;
password: string;
curStatus: any;
createDate: number;
}
export class CommonError extends Error {
code:number
errmsg:number
constructor(msg,code=500) {
code: number
errmsg: number
constructor(msg, code = 500) {
super(msg)
this.errmsg = msg;
this.code = code;
this.errmsg = msg
this.code = code
}
}

View File

@ -0,0 +1,55 @@
import { Collection, MongoClient, ObjectId } from 'mongodb'
import { getMongoMemoryUri } from './getMongoMemoryUri'
import { CommonError } from '../types'
class MongoService {
isInit: boolean
client: MongoClient
dbName: string
constructor({ url, dbName }) {
this.client = new MongoClient(url);
this.dbName = dbName;
}
async getCollection({ collectionName }): Promise<Collection> {
if (process.env.SERVER_ENV === 'local') {
const uri = await getMongoMemoryUri()
this.client = new MongoClient(uri);
}
try {
// 设置一个6秒的计时器
const timeoutPromise = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('连接超时'));
}, 6000); // 6秒超时
});
await Promise.race([this.client.connect(), timeoutPromise])
} catch (error) {
throw new CommonError('数据库连接错误:' + error.message)
}
try {
return this.client.db(this.dbName).collection(collectionName)
} catch (error) {
throw new CommonError(`get collection ${collectionName} error`)
}
}
convertId2StringByDoc(doc: any): any {
doc._id = doc._id.toString()
return doc
}
convertId2StringByList(list: Array<any>): Array<any> {
return list.map(e => {
return this.convertId2StringByDoc(e)
})
}
getObjectIdByStr(str: string) {
return new ObjectId(str)
}
}
export default MongoService

View File

@ -6,7 +6,7 @@ import { Message } from 'element-ui';
const instance = axios.create({
baseURL: '/api',
timeout: 3000,
timeout: 10000,
});
instance.interceptors.response.use(

View File

@ -117,6 +117,11 @@ export default {
if (res.code === CODE_MAP.SUCCESS) {
this.total = res.data.count;
this.data = res.data.data;
} else {
this.$message({
type: 'error',
message: res.errmsg,
});
}
} catch (error) {
this.$message({