feat: 服务端配置优化 (#4)
This commit is contained in:
parent
de2deeb49c
commit
0fda6cdc0c
@ -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 })
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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> = []
|
||||
|
@ -1,7 +1,7 @@
|
||||
const config = {
|
||||
mongo: {
|
||||
url: process.env.xiaojuSurveyMongoUrl || 'mongodb://localhost:27017',
|
||||
dbName: 'surveyEengine'
|
||||
dbName: 'xiaojuSurvey',
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 })
|
@ -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'
|
||||
|
||||
|
@ -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 }) {
|
||||
|
@ -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";
|
||||
|
@ -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 }) {
|
||||
|
@ -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'
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
const config = {
|
||||
mongo:{
|
||||
url: process.env.xiaojuSurveyMongoUrl ||'mongodb://localhost:27017',
|
||||
dbName:'surveyEengine'
|
||||
dbName:'xiaojuSurvey',
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 })
|
@ -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}) {
|
||||
|
@ -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'
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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')
|
||||
|
@ -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}) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
const config = {
|
||||
mongo:{
|
||||
url: process.env.xiaojuSurveyMongoUrl ||'mongodb://localhost:27017',
|
||||
dbName:'surveyEengine'
|
||||
dbName:'xiaojuSurvey',
|
||||
},
|
||||
jwt:{
|
||||
secret: process.env.xiaojuSurveyJwtSecret || 'xiaojuSurveyJwtSecret',
|
||||
|
@ -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 })
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
55
server/src/utils/mongoService.ts
Normal file
55
server/src/utils/mongoService.ts
Normal 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
|
@ -6,7 +6,7 @@ import { Message } from 'element-ui';
|
||||
|
||||
const instance = axios.create({
|
||||
baseURL: '/api',
|
||||
timeout: 3000,
|
||||
timeout: 10000,
|
||||
});
|
||||
|
||||
instance.interceptors.response.use(
|
||||
|
@ -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({
|
||||
|
Loading…
Reference in New Issue
Block a user