diff --git a/examples/app/multi-app.ts b/examples/app/multi-app.ts new file mode 100644 index 0000000000..ed66861cb8 --- /dev/null +++ b/examples/app/multi-app.ts @@ -0,0 +1,62 @@ +import { Application } from '@nocobase/server'; +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, + tablePrefix: process.env.DB_TABLE_PREFIX, + }, + resourcer: { + prefix: '/api', + }, + plugins: [], +}); + +if (require.main === module) { + app.runAsCLI(); +} + +const subApp1 = app.appManager.createApplication('sub1', { + database: app.db, + 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; +}); + +export default app; + +// http://localhost:13000/api/test:list +// http://localhost:13000/sub1/api/test:list diff --git a/examples/app/single-app.ts b/examples/app/single-app.ts new file mode 100644 index 0000000000..14911da0d4 --- /dev/null +++ b/examples/app/single-app.ts @@ -0,0 +1,37 @@ +import { Application } from '@nocobase/server'; + +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, + tablePrefix: process.env.DB_TABLE_PREFIX, + }, + resourcer: { + prefix: '/api', + }, + plugins: [], +}); + +app.resource({ + name: 'test', + actions: { + async list(ctx) { + ctx.body = 'test list'; + }, + }, +}); + +if (require.main === module) { + app.runAsCLI(); +} + +export default app; + +// http://localhost:13000/api/test:list \ No newline at end of file diff --git a/package.json b/package.json index 970bb5de08..19de3e4eb3 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "clean": "nocobase clean", "version:alpha": "lerna version prerelease --preid alpha --force-publish=* --no-git-tag-version -m \"chore(versions): publish packages %s\"", "release:force": "lerna publish from-package --yes", - "release": "lerna publish" + "release": "lerna publish", + "run:example": "ts-node-dev -r dotenv/config" }, "resolutions": { "@types/react": "^17.0.0", diff --git a/packages/app/server/src/index.ts b/packages/app/server/src/index.ts index eae2f074d4..2c9561c389 100644 --- a/packages/app/server/src/index.ts +++ b/packages/app/server/src/index.ts @@ -4,7 +4,7 @@ import config from './config'; const app = new Application(config); if (require.main === module) { - app.parse(); + app.runAsCLI(); } export default app; diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index 39cd9ed4d6..11a99b8dc6 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -3,7 +3,7 @@ import { registerActions } from '@nocobase/actions'; import Database, { Collection, CollectionOptions, IDatabaseOptions } from '@nocobase/database'; import Resourcer, { ResourceOptions } from '@nocobase/resourcer'; import { applyMixins, AsyncEmitter } from '@nocobase/utils'; -import { Command, CommandOptions } from 'commander'; +import { Command, CommandOptions, ParseOptions } from 'commander'; import { Server } from 'http'; import { i18n, InitOptions } from 'i18next'; import Koa from 'koa'; @@ -247,8 +247,12 @@ export class Application exten } async parse(argv = process.argv) { + return this.runAsCLI(argv); + } + + async runAsCLI(argv?: readonly string[], options?: ParseOptions) { await this.load(); - return this.cli.parseAsync(argv); + return this.cli.parseAsync(argv, options); } async start(options: StartOptions = {}) {