2022-08-16 14:41:29 +08:00
|
|
|
|
/*
|
|
|
|
|
# 支持多应用(子应用)
|
|
|
|
|
|
|
|
|
|
yarn run:example app/multi-app start
|
|
|
|
|
|
|
|
|
|
curl http://localhost:13000/api/test:list
|
|
|
|
|
curl http://localhost:13000/sub1/api/test:list
|
|
|
|
|
*/
|
2022-08-08 14:42:48 +08:00
|
|
|
|
import { Application } from '@nocobase/server';
|
2022-09-29 21:05:31 +08:00
|
|
|
|
import { uid } from '@nocobase/utils';
|
2022-08-08 14:42:48 +08:00
|
|
|
|
import { IncomingMessage } from 'http';
|
|
|
|
|
|
|
|
|
|
const app = new Application({
|
|
|
|
|
database: {
|
|
|
|
|
logging: process.env.DB_LOGGING === 'on' ? console.log : false,
|
|
|
|
|
dialect: process.env.DB_DIALECT as any,
|
|
|
|
|
storage: process.env.DB_STORAGE,
|
|
|
|
|
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 as any,
|
|
|
|
|
timezone: process.env.DB_TIMEZONE,
|
2022-09-29 21:05:31 +08:00
|
|
|
|
tablePrefix: `t_${uid()}_`,
|
2022-08-08 14:42:48 +08:00
|
|
|
|
},
|
|
|
|
|
resourcer: {
|
|
|
|
|
prefix: '/api',
|
|
|
|
|
},
|
2022-09-29 21:05:31 +08:00
|
|
|
|
acl: false,
|
2022-08-08 14:42:48 +08:00
|
|
|
|
plugins: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const subApp1 = app.appManager.createApplication('sub1', {
|
|
|
|
|
database: app.db,
|
2022-09-29 21:05:31 +08:00
|
|
|
|
acl: false,
|
2022-08-08 14:42:48 +08:00
|
|
|
|
resourcer: {
|
|
|
|
|
prefix: '/sub1/api/',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.resourcer.define({
|
|
|
|
|
name: 'test',
|
|
|
|
|
actions: {
|
|
|
|
|
async list(ctx) {
|
|
|
|
|
ctx.body = 'test list';
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
subApp1.resourcer.define({
|
|
|
|
|
name: 'test',
|
|
|
|
|
actions: {
|
|
|
|
|
async list(ctx) {
|
|
|
|
|
ctx.body = 'sub1 test list';
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.appManager.setAppSelector((req: IncomingMessage) => {
|
|
|
|
|
if (req.url.startsWith('/sub1/api')) {
|
|
|
|
|
return subApp1;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-16 14:41:29 +08:00
|
|
|
|
if (require.main === module) {
|
|
|
|
|
app.runAsCLI();
|
|
|
|
|
}
|
2022-08-08 14:42:48 +08:00
|
|
|
|
|
2022-08-16 14:41:29 +08:00
|
|
|
|
export default app;
|