feat: improve code (#350)

This commit is contained in:
chenos 2022-04-30 23:41:01 +08:00 committed by GitHub
parent d2d8bb6e18
commit 69da6a340f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 10 deletions

View File

@ -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>;

View File

@ -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);
});
});

View File

@ -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);

View File

@ -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',

View File

@ -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;

View File

@ -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,