2020-11-11 15:23:39 +08:00
|
|
|
import Api from '../../../server/src';
|
|
|
|
import dotenv from 'dotenv';
|
|
|
|
import path from 'path';
|
2020-11-13 22:01:14 +08:00
|
|
|
import actions from '../../../actions/src';
|
|
|
|
import associated from '../../../actions/src/middlewares/associated';
|
2020-12-12 16:36:02 +08:00
|
|
|
import { Op } from 'sequelize';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
|
|
|
const sync = {
|
2020-12-20 12:52:15 +08:00
|
|
|
force: false,
|
2020-11-11 15:23:39 +08:00
|
|
|
alter: {
|
2020-12-20 12:52:15 +08:00
|
|
|
drop: false,
|
2020-11-11 15:23:39 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-12-15 09:58:42 +08:00
|
|
|
console.log('process.env.NOCOBASE_ENV', process.env.NOCOBASE_ENV);
|
|
|
|
|
2020-11-11 15:23:39 +08:00
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
const api = Api.create({
|
|
|
|
database: {
|
|
|
|
username: process.env.DB_USER,
|
|
|
|
password: process.env.DB_PASSWORD,
|
|
|
|
database: process.env.DB_DATABASE,
|
|
|
|
host: process.env.DB_HOST,
|
|
|
|
port: process.env.DB_PORT,
|
|
|
|
dialect: process.env.DB_DIALECT,
|
|
|
|
dialectOptions: {
|
|
|
|
charset: 'utf8mb4',
|
|
|
|
collate: 'utf8mb4_unicode_ci',
|
|
|
|
},
|
|
|
|
logging: false,
|
|
|
|
define: {},
|
|
|
|
sync,
|
|
|
|
},
|
|
|
|
resourcer: {
|
|
|
|
prefix: '/api',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-18 15:33:34 +08:00
|
|
|
api.resourcer.use(async (ctx: actions.Context, next) => {
|
|
|
|
const token = ctx.get('Authorization').replace(/^Bearer\s+/gi, '');
|
|
|
|
// console.log('user check', ctx.action.params.actionName);
|
|
|
|
// const { actionName } = ctx.action.params;
|
|
|
|
if (!token) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const User = ctx.db.getModel('users');
|
|
|
|
const user = await User.findOne({
|
|
|
|
where: {
|
|
|
|
token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!user) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
ctx.state.currentUser = user;
|
|
|
|
// console.log('ctx.state.currentUser', ctx.state.currentUser);
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
2020-12-15 09:58:42 +08:00
|
|
|
api.resourcer.use(async (ctx: actions.Context, next) => {
|
2020-12-20 12:52:15 +08:00
|
|
|
const { resourceName, fields = {} } = ctx.action.params;
|
2020-12-08 10:02:41 +08:00
|
|
|
const table = ctx.db.getTable(resourceName);
|
2020-12-12 16:36:02 +08:00
|
|
|
// ctx.state.developerMode = {[Op.not]: null};
|
2020-12-08 14:33:28 +08:00
|
|
|
ctx.state.developerMode = false;
|
2020-12-12 16:36:02 +08:00
|
|
|
if (table && table.hasField('developerMode') && ctx.state.developerMode === false) {
|
2020-12-08 14:33:28 +08:00
|
|
|
ctx.action.setParam('filter.developerMode', ctx.state.developerMode);
|
2020-12-08 10:02:41 +08:00
|
|
|
}
|
2020-12-15 09:58:42 +08:00
|
|
|
if (table) {
|
2020-12-20 12:52:15 +08:00
|
|
|
const except = fields.except || [];
|
|
|
|
const appends = fields.appends || [];
|
2020-12-15 09:58:42 +08:00
|
|
|
for (const [name, field] of table.getFields()) {
|
|
|
|
if (field.options.hidden) {
|
|
|
|
except.push(field.options.name);
|
|
|
|
}
|
2020-12-18 15:33:34 +08:00
|
|
|
if (field.options.appends) {
|
|
|
|
appends.push(field.options.name);
|
|
|
|
}
|
2020-12-15 09:58:42 +08:00
|
|
|
}
|
|
|
|
if (except.length) {
|
|
|
|
ctx.action.setParam('fields.except', except);
|
|
|
|
}
|
2020-12-18 15:33:34 +08:00
|
|
|
if (appends.length) {
|
|
|
|
ctx.action.setParam('fields.appends', appends);
|
|
|
|
}
|
2020-12-18 19:54:53 +08:00
|
|
|
console.log('ctx.action.params.fields', ctx.action.params.fields, except, appends);
|
2020-12-15 09:58:42 +08:00
|
|
|
}
|
2020-12-08 10:02:41 +08:00
|
|
|
await next();
|
|
|
|
});
|
2020-12-15 14:32:56 +08:00
|
|
|
|
|
|
|
api.resourcer.use(async (ctx: actions.Context, next) => {
|
|
|
|
const { resourceName, viewName, filter } = ctx.action.params;
|
|
|
|
// TODO: 需要补充默认视图的情况
|
|
|
|
let view: any;
|
|
|
|
if (viewName) {
|
|
|
|
view = await ctx.db.getModel('views').findOne({
|
|
|
|
where: {
|
|
|
|
collection_name: resourceName,
|
|
|
|
name: viewName,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const viewFilter = view.get('filter');
|
|
|
|
if (viewFilter) {
|
|
|
|
const args = [viewFilter, filter].filter(Boolean);
|
|
|
|
ctx.action.setParam('filter', {and: args});
|
|
|
|
console.log(ctx.action.params.filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
});
|
2020-12-18 19:54:53 +08:00
|
|
|
|
2020-11-13 22:01:14 +08:00
|
|
|
api.resourcer.use(associated);
|
2020-12-02 13:48:19 +08:00
|
|
|
api.resourcer.registerActionHandlers({...actions.common, ...actions.associate});
|
2020-11-13 22:01:14 +08:00
|
|
|
|
2020-12-18 19:54:53 +08:00
|
|
|
api.registerPlugin('plugin-collections', [path.resolve(__dirname, '../../../plugin-collections'), {}]);
|
|
|
|
api.registerPlugin('plugin-pages', [path.resolve(__dirname, '../../../plugin-pages'), {}]);
|
|
|
|
api.registerPlugin('plugin-users', [path.resolve(__dirname, '../../../plugin-users'), {}]);
|
2020-12-08 10:02:41 +08:00
|
|
|
|
2020-12-18 19:54:53 +08:00
|
|
|
(async () => {
|
|
|
|
await api.loadPlugins();
|
2020-11-11 15:23:39 +08:00
|
|
|
api.listen(process.env.HTTP_PORT, () => {
|
|
|
|
console.log(`http://localhost:${process.env.HTTP_PORT}/`);
|
|
|
|
});
|
|
|
|
})();
|