yapi/server/install.js

143 lines
3.9 KiB
JavaScript
Raw Normal View History

const fs = require('fs-extra');
const yapi = require('./yapi.js');
const commons = require('./utils/commons');
const dbModule = require('./utils/db.js');
const userModel = require('./models/user.js');
const mongoose = require('mongoose');
2017-07-11 16:50:17 +08:00
yapi.commons = commons;
yapi.connect = dbModule.connect();
2017-08-23 16:11:43 +08:00
function install() {
2017-09-26 21:05:20 +08:00
let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
2017-07-11 16:50:17 +08:00
2017-09-26 21:05:20 +08:00
if (exist) {
throw new Error('init.lock文件已存在请确认您是否已安装。如果需要重新安装请删掉init.lock文件');
2017-07-11 16:50:17 +08:00
}
2017-09-26 21:05:20 +08:00
setupSql();
2017-07-11 16:50:17 +08:00
}
function setupSql() {
let userInst = yapi.getInst(userModel);
2017-07-11 16:50:17 +08:00
let passsalt = yapi.commons.randStr();
let result = userInst.save({
2017-07-17 11:01:05 +08:00
username: yapi.WEBCONFIG.adminAccount.substr(0, yapi.WEBCONFIG.adminAccount.indexOf('@')),
2017-07-11 16:50:17 +08:00
email: yapi.WEBCONFIG.adminAccount,
2017-09-27 10:33:06 +08:00
password: yapi.commons.generatePassword('ymfe.org', passsalt),
2017-07-11 16:50:17 +08:00
passsalt: passsalt,
role: 'admin',
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
});
2017-09-26 21:05:20 +08:00
yapi.connect.then(function () {
2017-08-23 16:11:43 +08:00
let userCol = mongoose.connection.db.collection('user')
2017-10-18 10:04:16 +08:00
userCol.createIndex({
2017-08-23 16:11:43 +08:00
username: 1
})
2017-10-18 10:04:16 +08:00
userCol.createIndex({
2017-08-23 16:11:43 +08:00
email: 1
}, {
unique: true
})
let projectCol = mongoose.connection.db.collection('project')
2017-10-18 10:04:16 +08:00
projectCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
2017-10-18 10:04:16 +08:00
projectCol.createIndex({
2017-08-23 16:11:43 +08:00
name: 1
})
2017-10-18 10:04:16 +08:00
projectCol.createIndex({
2017-08-23 16:11:43 +08:00
group_id: 1
})
let logCol = mongoose.connection.db.collection('log')
2017-10-18 10:04:16 +08:00
logCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
2017-10-18 10:04:16 +08:00
logCol.createIndex({
2017-08-23 16:11:43 +08:00
typeid: 1,
type: 1
})
let interfaceColCol = mongoose.connection.db.collection('interface_col')
2017-10-18 10:04:16 +08:00
interfaceColCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
2017-10-18 10:04:16 +08:00
interfaceColCol.createIndex({
2017-08-23 16:11:43 +08:00
project_id: 1
})
let interfaceCatCol = mongoose.connection.db.collection('interface_cat')
2017-10-18 10:04:16 +08:00
interfaceCatCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
2017-10-18 10:04:16 +08:00
interfaceCatCol.createIndex({
2017-08-23 16:11:43 +08:00
project_id: 1
})
let interfaceCaseCol = mongoose.connection.db.collection('interface_case')
2017-10-18 10:04:16 +08:00
interfaceCaseCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
2017-10-18 10:04:16 +08:00
interfaceCaseCol.createIndex({
2017-08-23 16:11:43 +08:00
col_id: 1
})
2017-10-18 10:04:16 +08:00
interfaceCaseCol.createIndex({
2017-08-23 16:11:43 +08:00
project_id: 1
})
let interfaceCol = mongoose.connection.db.collection('interface')
2017-10-18 10:04:16 +08:00
interfaceCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
2017-10-18 10:04:16 +08:00
interfaceCol.createIndex({
2017-08-23 16:11:43 +08:00
path: 1,
method: 1
})
2017-10-18 10:04:16 +08:00
interfaceCol.createIndex({
2017-08-23 16:11:43 +08:00
project_id: 1
})
let groupCol = mongoose.connection.db.collection('group')
2017-10-18 10:04:16 +08:00
groupCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
2017-10-18 10:04:16 +08:00
groupCol.createIndex({
2017-08-23 16:11:43 +08:00
group_name: 1
})
let avatarCol = mongoose.connection.db.collection('avatar')
2017-10-18 10:04:16 +08:00
avatarCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
let followCol = mongoose.connection.db.collection('follow')
2017-10-18 10:04:16 +08:00
followCol.createIndex({
2017-08-23 16:11:43 +08:00
uid: 1
})
2017-10-18 10:04:16 +08:00
followCol.createIndex({
2017-08-23 16:11:43 +08:00
project_id: 1
})
2017-09-26 21:05:20 +08:00
2017-08-23 16:11:43 +08:00
result.then(function () {
2017-09-26 21:05:20 +08:00
fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
2017-09-27 10:33:06 +08:00
console.log(`初始化管理员账号成功,账号名:"${yapi.WEBCONFIG.adminAccount}",密码:"ymfe.org"`); // eslint-disable-line
2017-08-23 16:11:43 +08:00
process.exit(0);
}, function (err) {
2017-09-27 10:33:06 +08:00
throw new Error(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 失败, ${err.message}`); // eslint-disable-line
2017-08-23 16:11:43 +08:00
});
2017-09-26 21:05:20 +08:00
2017-08-23 16:11:43 +08:00
2017-09-27 10:33:06 +08:00
}).catch(function(err){
throw new Error(err.message);
2017-09-26 21:08:02 +08:00
})
2017-08-23 16:11:43 +08:00
2017-07-11 16:50:17 +08:00
}
install();