feat: improve code (#350)
This commit is contained in:
parent
d2d8bb6e18
commit
69da6a340f
@ -45,6 +45,10 @@ import apiClient from './apiClient';
|
|||||||
apiClient.axios.interceptors.response.use(
|
apiClient.axios.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => response,
|
||||||
(error) => {
|
(error) => {
|
||||||
|
const redirectTo = error?.response?.data?.redirectTo;
|
||||||
|
if (redirectTo) {
|
||||||
|
return window.location.href = redirectTo;
|
||||||
|
}
|
||||||
notification.error({
|
notification.error({
|
||||||
message: error?.response?.data?.errors?.map?.((error: any) => {
|
message: error?.response?.data?.errors?.map?.((error: any) => {
|
||||||
return <div>{error.message}</div>;
|
return <div>{error.message}</div>;
|
||||||
|
@ -99,5 +99,10 @@ describe('multiple application', () => {
|
|||||||
app: 'sub2',
|
app: 'sub2',
|
||||||
});
|
});
|
||||||
expect(response.statusCode).toEqual(200);
|
expect(response.statusCode).toEqual(200);
|
||||||
|
|
||||||
|
response = await app.agent().resource('test').test({
|
||||||
|
app: 'sub3',
|
||||||
|
});
|
||||||
|
expect(response.statusCode).toEqual(404);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
|
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
import http, { IncomingMessage } from 'http';
|
import http, { IncomingMessage, ServerResponse } from 'http';
|
||||||
import Application, { ApplicationOptions } from './application';
|
import Application, { ApplicationOptions } from './application';
|
||||||
|
|
||||||
type AppSelector = (req: IncomingMessage) => Application | string | undefined | null;
|
type AppSelector = (req: IncomingMessage) => Application | string | undefined | null;
|
||||||
@ -62,11 +62,24 @@ export class AppManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
callback() {
|
callback() {
|
||||||
return async (req, res) => {
|
return async (req: IncomingMessage, res: ServerResponse) => {
|
||||||
let handleApp = this.appSelector(req) || this.app;
|
let handleApp: any = this.appSelector(req) || this.app;
|
||||||
|
|
||||||
if (typeof handleApp === 'string') {
|
if (typeof handleApp === 'string') {
|
||||||
handleApp = (await this.getApplication(handleApp)) || this.app;
|
handleApp = await this.getApplication(handleApp);
|
||||||
|
if (!handleApp) {
|
||||||
|
res.statusCode = 404;
|
||||||
|
return res.end(
|
||||||
|
JSON.stringify({
|
||||||
|
redirectTo: process.env.APP_NOT_FOUND_REDIRECT_TO,
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
message: 'Not Found',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleApp.callback()(req, res);
|
handleApp.callback()(req, res);
|
||||||
|
@ -21,6 +21,21 @@ export default defineCollection({
|
|||||||
'x-read-pretty': true,
|
'x-read-pretty': true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
name: 'status',
|
||||||
|
interface: 'radioGroup',
|
||||||
|
defaultValue: 'pending',
|
||||||
|
uiSchema: {
|
||||||
|
type: 'string',
|
||||||
|
title: '{{t("Application status")}}',
|
||||||
|
'x-component': 'Radio.Group',
|
||||||
|
enum: [
|
||||||
|
{ label: '创建中', value: 'pending' },
|
||||||
|
{ label: '运行中', value: 'running' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
type: 'json',
|
type: 'json',
|
||||||
name: 'options',
|
name: 'options',
|
||||||
|
@ -30,14 +30,16 @@ export class ApplicationModel extends Model {
|
|||||||
const appName = this.get('name') as string;
|
const appName = this.get('name') as string;
|
||||||
const appOptions = (this.get('options') as any) || {};
|
const appOptions = (this.get('options') as any) || {};
|
||||||
|
|
||||||
|
const AppModel = this.constructor as typeof ApplicationModel;
|
||||||
|
|
||||||
const app = mainApp.appManager.createApplication(appName, {
|
const app = mainApp.appManager.createApplication(appName, {
|
||||||
...ApplicationModel.initOptions(appName, mainApp),
|
...AppModel.initOptions(appName, mainApp),
|
||||||
...appOptions,
|
...appOptions,
|
||||||
});
|
});
|
||||||
|
|
||||||
// create database before installation if it not exists
|
// create database before installation if it not exists
|
||||||
app.on('beforeInstall', async function createDatabase() {
|
app.on('beforeInstall', async function createDatabase() {
|
||||||
const { host, port, username, password, database, dialect } = ApplicationModel.getDatabaseConfig(app);
|
const { host, port, username, password, database, dialect } = AppModel.getDatabaseConfig(app);
|
||||||
|
|
||||||
if (dialect === 'mysql') {
|
if (dialect === 'mysql') {
|
||||||
const mysql = require('mysql2/promise');
|
const mysql = require('mysql2/promise');
|
||||||
@ -66,11 +68,24 @@ export class ApplicationModel extends Model {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await ApplicationModel.handleAppStart(app, options);
|
await AppModel.handleAppStart(app, options);
|
||||||
|
|
||||||
|
await AppModel.update(
|
||||||
|
{
|
||||||
|
status: 'running',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
transaction: options.transaction,
|
||||||
|
where: {
|
||||||
|
[AppModel.primaryKeyAttribute]: this.get(AppModel.primaryKeyAttribute),
|
||||||
|
},
|
||||||
|
hooks: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static initOptions(appName: string, mainApp: Application) {
|
static initOptions(appName: string, mainApp: Application) {
|
||||||
const rawDatabaseOptions = ApplicationModel.getDatabaseConfig(mainApp);
|
const rawDatabaseOptions = this.getDatabaseConfig(mainApp);
|
||||||
|
|
||||||
if (rawDatabaseOptions.dialect === 'sqlite') {
|
if (rawDatabaseOptions.dialect === 'sqlite') {
|
||||||
const mainAppStorage = rawDatabaseOptions.storage;
|
const mainAppStorage = rawDatabaseOptions.storage;
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
import { Plugin } from '@nocobase/server';
|
import { AppManager, InstallOptions, Plugin } from '@nocobase/server';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import { ApplicationModel } from './models/application';
|
import { ApplicationModel } from './models/application';
|
||||||
import { AppManager } from '@nocobase/server';
|
|
||||||
|
|
||||||
export class PluginMultiAppManager extends Plugin {
|
export class PluginMultiAppManager extends Plugin {
|
||||||
getName(): string {
|
getName(): string {
|
||||||
return this.getPackageName(__dirname);
|
return this.getPackageName(__dirname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async install(options?: InstallOptions) {
|
||||||
|
const repo = this.db.getRepository<any>('collections');
|
||||||
|
if (repo) {
|
||||||
|
await repo.db2cm('applications');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
this.db.registerModels({
|
this.db.registerModels({
|
||||||
ApplicationModel,
|
ApplicationModel,
|
||||||
|
Loading…
Reference in New Issue
Block a user