tachybase_todo/packages/api/src/app.ts

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-01-04 13:17:16 +08:00
import path from 'path';
2021-03-23 10:53:11 +08:00
import dotenv from 'dotenv';
import Api from '../../server/src';
import actions from '../../actions/src';
import associated from '../../actions/src/middlewares/associated';
2021-03-26 10:06:36 +08:00
import { DatabaseOptions } from '@nocobase/database';
2021-01-04 13:17:16 +08:00
// @ts-ignore
const sync = global.sync || {
force: true,
2021-01-04 13:17:16 +08:00
alter: {
drop: true,
2021-01-04 13:17:16 +08:00
},
};
console.log('process.env.NOCOBASE_ENV', process.env.NOCOBASE_ENV);
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',
},
2021-03-26 10:06:36 +08:00
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000,
},
logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
2021-01-04 13:17:16 +08:00
define: {},
sync,
},
resourcer: {
prefix: '/api',
},
});
api.resourcer.use(associated);
2021-03-28 13:34:51 +08:00
api.resourcer.registerActionHandlers({ ...actions.common, ...actions.associate });
2021-01-04 13:17:16 +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();
// });
2021-01-04 13:17:16 +08:00
2021-03-23 10:53:11 +08:00
api.registerPlugin('plugin-collections', [path.resolve(__dirname, '../../plugin-collections'), {}]);
api.registerPlugin('plugin-action-logs', [path.resolve(__dirname, '../../plugin-action-logs'), {}]);
api.registerPlugin('plugin-pages', [path.resolve(__dirname, '../../plugin-pages'), {}]);
api.registerPlugin('plugin-users', [path.resolve(__dirname, '../../plugin-users'), {}]);
api.registerPlugin('plugin-file-manager', [path.resolve(__dirname, '../../plugin-file-manager'), {}]);
api.registerPlugin('plugin-permissions', [path.resolve(__dirname, '../../plugin-permissions'), {}]);
api.registerPlugin('plugin-automations', [path.resolve(__dirname, '../../plugin-automations'), {}]);
api.registerPlugin('plugin-china-region', [path.resolve(__dirname, '../../plugin-china-region'), {}]);
2021-01-04 13:17:16 +08:00
export default api;