You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.4 KiB

2 years ago
'use strict';
const fs = require('fs');
const path = require('path');
const utils = require('./utils')
const routes = require('./routes');
1 year ago
const redisConnect = require('./service/redis')
2 years ago
const socketConect = require('./service/socket')
2 years ago
const paasRequest = require('./service/paasRequest');
2 years ago
const authenticator = require('./middlewares/authenticator');
//const clickHouseClient = require('./service/clickHouseClient')
const schedule = require('./schedule')
// const apiLog = require('./middlewares/api-log');
2 years ago
module.exports.entry = function (app, router, opts) {
app.fs.logger.log('info', '[FS-AUTH]', 'Inject auth and api mv into router.');
app.fs.api = app.fs.api || {};
app.fs.opts = opts || {};
app.fs.utils = app.fs.utils || {};
app.fs.api.authAttr = app.fs.api.authAttr || {};
app.fs.api.logAttr = app.fs.api.logAttr || {};
// 顺序固定 ↓
1 year ago
redisConnect(app, opts)
2 years ago
socketConect(app, opts)
// 实例其他平台请求方法
2 years ago
paasRequest(app, opts)
2 years ago
// clickHouse 数据库 client
//clickHouseClient(app, opts)
// 工具类函数
utils(app, opts)
// 定时任务
schedule(app, opts)
//鉴权中间件
router.use(authenticator(app, opts));
// 日志记录
// router.use(apiLog(app, opts));
router = routes(app, router, opts);
};
module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Sequelize, models: {} }
// 模型关系摘出来 初始化之后再定义关系才行
fs.readdirSync(path.join(__dirname, '/models')).forEach((filename) => {
require(`./models/${filename}`)(dc)
});
const { User, UserResource, Resource, OperationLog, } = dc.models;
2 years ago
User.belongsToMany(Resource, { through: UserResource, foreignKey: 'userId', otherKey: 'resourceCode' });
Resource.belongsToMany(User, { through: UserResource, foreignKey: 'resourceCode', otherKey: 'userId' });
2 years ago
UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' });
2 years ago
UserResource.belongsTo(Resource, { foreignKey: 'resourceCode', targetKey: 'code' });
Resource.hasMany(UserResource, { foreignKey: 'resourceCode', sourceKey: 'code' });
Resource.hasMany(Resource, { foreignKey: 'parentCode', sourceKey: 'code' });
2 years ago
OperationLog.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(OperationLog, { foreignKey: 'userId', sourceKey: 'id' });
2 years ago
};