2021-01-13 16:23:15 +08:00
|
|
|
import api from './app';
|
2021-04-06 20:05:56 +08:00
|
|
|
import actions from '@nocobase/actions';
|
|
|
|
import send from 'koa-send';
|
|
|
|
import serve from 'koa-static';
|
2020-12-18 15:33:34 +08:00
|
|
|
|
2020-12-15 09:58:42 +08:00
|
|
|
api.resourcer.use(async (ctx: actions.Context, next) => {
|
2021-01-01 19:12:53 +08:00
|
|
|
const { actionName, resourceField, resourceName, fields = {} } = ctx.action.params;
|
2020-12-25 16:15:58 +08:00
|
|
|
const table = ctx.db.getTable(resourceField ? resourceField.options.target : 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) {
|
2021-01-20 08:33:47 +08:00
|
|
|
ctx.action.mergeParams({ filter: { "developerMode.$isFalsy": true } }, { filter: 'and' });
|
2020-12-08 10:02:41 +08:00
|
|
|
}
|
2021-01-01 19:12:53 +08:00
|
|
|
if (table && ['get', 'list'].includes(actionName)) {
|
2021-01-13 16:23:15 +08:00
|
|
|
const except = [];
|
|
|
|
const 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);
|
|
|
|
}
|
2021-01-07 09:31:26 +08:00
|
|
|
// if (field.options.appends) {
|
|
|
|
// appends.push(field.options.name);
|
|
|
|
// }
|
2020-12-15 09:58:42 +08:00
|
|
|
}
|
2021-01-13 16:23:15 +08:00
|
|
|
ctx.action.mergeParams({ fields: {
|
|
|
|
except,
|
|
|
|
appends
|
|
|
|
} }, { fields: 'append' });
|
2021-01-04 20:53:30 +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
|
|
|
|
2020-12-18 19:54:53 +08:00
|
|
|
(async () => {
|
|
|
|
await api.loadPlugins();
|
2021-01-06 17:38:08 +08:00
|
|
|
|
2021-01-07 09:31:26 +08:00
|
|
|
api.database.getModel('users').addHook('beforeUpdate', function (model) {
|
|
|
|
console.log('users.beforeUpdate', model.get(), model.changed('password' as any));
|
|
|
|
});
|
|
|
|
|
2021-01-06 17:38:08 +08:00
|
|
|
api.resourcer.use(async (ctx, next) => {
|
|
|
|
if (process.env.NOCOBASE_ENV !== 'demo') {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const currentUser = ctx.state.currentUser;
|
|
|
|
if (currentUser && currentUser.username === 'admin') {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const { actionName } = ctx.action.params;
|
2021-03-29 14:01:54 +08:00
|
|
|
if (['create', 'update', 'destroy', 'sort', 'upload'].includes(actionName)) {
|
2021-01-06 17:38:08 +08:00
|
|
|
ctx.body = {
|
|
|
|
data: {},
|
|
|
|
meta: {},
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
2020-12-31 21:04:03 +08:00
|
|
|
await api.database.getModel('collections').load({skipExisting: true});
|
2021-01-27 14:02:46 +08:00
|
|
|
await api.database.getModel('collections').load({where: {
|
|
|
|
name: 'users',
|
|
|
|
}});
|
2021-02-07 17:39:47 +08:00
|
|
|
await api.database.getModel('automations').load();
|
2021-04-06 20:05:56 +08:00
|
|
|
|
|
|
|
api.use(async (ctx, next) => {
|
|
|
|
if (!process.env.APP_DIST || !process.env.USE_STATIC_SERVER || process.env.USE_STATIC_SERVER === 'false') {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
await serve(process.env.APP_DIST)(ctx, next);
|
|
|
|
if (ctx.status == 404) {
|
|
|
|
return send(ctx, 'index.html', { root: process.env.APP_DIST });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-22 13:33:57 +08:00
|
|
|
api.listen(process.env.API_PORT, () => {
|
|
|
|
console.log(`http://localhost:${process.env.API_PORT}/`);
|
2020-11-11 15:23:39 +08:00
|
|
|
});
|
|
|
|
})();
|