yapi/server/yapi.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

import path from 'path';
import fs from 'fs-extra';
2017-07-11 16:50:17 +08:00
import nodemailer from 'nodemailer';
2017-07-27 20:22:54 +08:00
import config from '../../config.json';
let insts = new Map();
2017-07-11 16:50:17 +08:00
let mail;
2017-07-05 17:59:53 +08:00
const WEBROOT = path.resolve(__dirname, '..'); //路径
const WEBROOT_SERVER = __dirname;
2017-07-27 14:21:22 +08:00
const WEBROOT_RUNTIME = path.resolve(__dirname, '../..');
const WEBROOT_LOG = path.join(WEBROOT_RUNTIME, 'log');
const WEBCONFIG = config;
2017-07-11 16:50:17 +08:00
fs.ensureDirSync(WEBROOT_LOG);
if (WEBCONFIG.mail) {
mail = nodemailer.createTransport(WEBCONFIG.mail);
2017-07-11 16:50:17 +08:00
}
/**
* 获取一个model实例如果不存在则创建一个新的返回
* @param {*} m class
* @example
* yapi.getInst(groupModel, arg1, arg2)
*/
function getInst(m, ...args) {
if (!insts.get(m)) {
insts.set(m, new m(args));
}
return insts.get(m);
}
function delInst(m) {
try {
insts.delete(m);
} catch (err) {
2017-07-27 19:49:26 +08:00
console.error(err); // eslint-disable-line
}
}
2017-07-11 16:50:17 +08:00
let r = {
fs: fs,
path: path,
WEBROOT: WEBROOT,
WEBROOT_SERVER: WEBROOT_SERVER,
WEBROOT_RUNTIME: WEBROOT_RUNTIME,
WEBROOT_LOG: WEBROOT_LOG,
WEBCONFIG: WEBCONFIG,
getInst: getInst,
delInst: delInst,
getInsts: insts
};
if (mail) r.mail = mail;
module.exports = r;