mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 07:38:13 +00:00
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(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
const redirectTo = error?.response?.data?.redirectTo;
|
||||
if (redirectTo) {
|
||||
return window.location.href = redirectTo;
|
||||
}
|
||||
notification.error({
|
||||
message: error?.response?.data?.errors?.map?.((error: any) => {
|
||||
return <div>{error.message}</div>;
|
||||
|
@ -99,5 +99,10 @@ describe('multiple application', () => {
|
||||
app: 'sub2',
|
||||
});
|
||||
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 EventEmitter from 'events';
|
||||
import http, { IncomingMessage } from 'http';
|
||||
import http, { IncomingMessage, ServerResponse } from 'http';
|
||||
import Application, { ApplicationOptions } from './application';
|
||||
|
||||
type AppSelector = (req: IncomingMessage) => Application | string | undefined | null;
|
||||
@ -62,11 +62,24 @@ export class AppManager extends EventEmitter {
|
||||
}
|
||||
|
||||
callback() {
|
||||
return async (req, res) => {
|
||||
let handleApp = this.appSelector(req) || this.app;
|
||||
return async (req: IncomingMessage, res: ServerResponse) => {
|
||||
let handleApp: any = this.appSelector(req) || this.app;
|
||||
|
||||
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);
|
||||
|
@ -21,6 +21,21 @@ export default defineCollection({
|
||||
'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',
|
||||
name: 'options',
|
||||
|
@ -30,14 +30,16 @@ export class ApplicationModel extends Model {
|
||||
const appName = this.get('name') as string;
|
||||
const appOptions = (this.get('options') as any) || {};
|
||||
|
||||
const AppModel = this.constructor as typeof ApplicationModel;
|
||||
|
||||
const app = mainApp.appManager.createApplication(appName, {
|
||||
...ApplicationModel.initOptions(appName, mainApp),
|
||||
...AppModel.initOptions(appName, mainApp),
|
||||
...appOptions,
|
||||
});
|
||||
|
||||
// create database before installation if it not exists
|
||||
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') {
|
||||
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) {
|
||||
const rawDatabaseOptions = ApplicationModel.getDatabaseConfig(mainApp);
|
||||
const rawDatabaseOptions = this.getDatabaseConfig(mainApp);
|
||||
|
||||
if (rawDatabaseOptions.dialect === 'sqlite') {
|
||||
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 { ApplicationModel } from './models/application';
|
||||
import { AppManager } from '@nocobase/server';
|
||||
|
||||
export class PluginMultiAppManager extends Plugin {
|
||||
getName(): string {
|
||||
return this.getPackageName(__dirname);
|
||||
}
|
||||
|
||||
async install(options?: InstallOptions) {
|
||||
const repo = this.db.getRepository<any>('collections');
|
||||
if (repo) {
|
||||
await repo.db2cm('applications');
|
||||
}
|
||||
}
|
||||
|
||||
async load() {
|
||||
this.db.registerModels({
|
||||
ApplicationModel,
|
||||
|
Loading…
Reference in New Issue
Block a user