feat: add dynamic app

This commit is contained in:
2024-11-22 02:13:12 +08:00
parent d71a574613
commit 40f42ca89b
12 changed files with 358 additions and 23 deletions

View File

@@ -12,3 +12,13 @@ export { bucketName };
if (!minioClient) {
throw new Error('Minio client not initialized');
}
// 验证权限
// (async () => {
// const bucketExists = await minioClient.bucketExists(bucketName);
// if (!bucketExists) {
// await minioClient.makeBucket(bucketName);
// }
// const res = await minioClient.putObject(bucketName, 'private/test/a.b', 'test');
// console.log('minio putObject', res);
// })();

View File

@@ -8,6 +8,7 @@ import { useFileStore } from '@abearxiong/use-file-store';
import { app, minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts';
import { hash } from 'crypto';
const cacheFilePath = useFileStore('cache-file', { needExists: true });
router.post('/api/micro-app/upload', async (req, res) => {
@@ -18,10 +19,11 @@ router.post('/api/micro-app/upload', async (req, res) => {
//
// 使用 formidable 解析 multipart/form-data
const form = new IncomingForm({
multiples: true, // 支持多文件上传
multiples: false, // 支持多文件上传
uploadDir: cacheFilePath, // 上传文件存储目录
allowEmptyFiles: true, // 允许空
minFileSize: 0, // 最小文件大小
maxFiles: 1, // 最大文件数量
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
keepExtensions: true, // 保留文件
hashAlgorithm: 'md5', // 文件哈希算法
@@ -52,31 +54,23 @@ router.post('/api/micro-app/upload', async (req, res) => {
fs.unlinkSync(file.filepath);
});
};
let appKey, version;
const { appKey: _appKey, version: _version } = fields;
let appKey, collection;
const { appKey: _appKey, collection: _collecion } = fields;
if (Array.isArray(_appKey)) {
appKey = _appKey?.[0];
} else {
appKey = _appKey;
}
if (Array.isArray(_version)) {
version = _version?.[0];
if (Array.isArray(_collecion)) {
collection = _collecion?.[0];
} else {
version = _version;
collection = _collecion;
}
appKey = appKey || 'micro-app';
// if (!appKey) {
// res.end(error('appKey is required'));
// clearFiles();
// return;
// }
// if (!version) {
// res.end(error('version is required'));
// clearFiles();
// return;
// }
console.log('Appkey', appKey, version);
collection = parseIfJson(collection);
appKey = appKey || 'micro-app';
console.log('Appkey', appKey);
console.log('collection', collection);
// 逐个处理每个上传的文件
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
const uploadResults = [];
@@ -86,7 +80,7 @@ router.post('/api/micro-app/upload', async (req, res) => {
const tempPath = file.filepath; // 文件上传时的临时路径
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// 比如 child2/b.txt
const minioPath = `/private/${tokenUser.username}/${appKey}/${relativePath}`;
const minioPath = `private/${tokenUser.username}/${appKey}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
@@ -97,17 +91,20 @@ router.post('/api/micro-app/upload', async (req, res) => {
uploadResults.push({
name: relativePath,
path: minioPath,
hash: file.hash,
size: file.size,
});
fs.unlinkSync(tempPath); // 删除临时文件
}
// 受控
const r = await app.call({
path: 'micro-app',
key: 'publish',
key: 'upload',
payload: {
token: token,
data: {
appKey,
collection,
files: uploadResults,
},
},
@@ -122,3 +119,10 @@ router.post('/api/micro-app/upload', async (req, res) => {
res.end(JSON.stringify(data));
});
});
function parseIfJson(collection: any): any {
try {
return JSON.parse(collection);
} catch (e) {
return collection;
}
}

View File

@@ -21,3 +21,5 @@ import './app-manager/index.ts';
import './file/index.ts';
import './packages/index.ts';
import './micro-app/index.ts';

View File

@@ -0,0 +1 @@
import './list.ts';

View File

@@ -0,0 +1,91 @@
import { app } from '@/app.ts';
import { MicroAppModel } from './models.ts';
import { appCheck, installApp } from './module/install-app.ts';
import { loadApp } from './module/load-app.ts';
app
.route({
path: 'micro-app',
key: 'upload',
middleware: ['auth'],
})
.define(async (ctx) => {
const { files, collection } = ctx.query?.data;
const { uid, username } = ctx.state.tokenUser;
const file = files[0];
console.log('File', files);
const { path, name, hash, size } = file;
const microApp = await MicroAppModel.create({
title: name,
description: '',
type: 'micro-app',
tags: [],
data: {
file: {
path,
size,
name,
hash,
},
collection,
},
uid,
share: false,
uname: username,
});
ctx.body = microApp;
})
.addTo(app);
// curl http://localhost:4002/api/router?path=micro-app&key=deploy
app
.route({
path: 'micro-app',
key: 'deploy',
})
.define(async (ctx) => {
// const { id, key} = ctx.query?.data;
// const id = '10f03411-85fc-4d37-a4d3-e32b15566a6c';
// const key = 'envision-cli';
const id = '7c54a6de-9171-4093-926d-67a035042c6c';
const key = 'mark';
if (!id) {
ctx.throw(400, 'Invalid id');
}
const microApp = await MicroAppModel.findByPk(id);
const { file } = microApp.data || {};
const path = file?.path;
if (!path) {
ctx.throw(404, 'Invalid path');
}
console.log('path', path);
const check = await appCheck({ key });
if (check) {
ctx.throw(400, 'App already exists, please remove it first');
}
await installApp({ path, key });
})
.addTo(app);
// curl http://localhost:4002/api/router?path=micro-app&key=load
app
.route({
path: 'micro-app',
key: 'load',
})
.define(async (ctx) => {
// const { key } = ctx.query?.data;
const key = 'mark';
try {
const main = await loadApp(key);
if (main?.loadApp) {
await main.loadApp(app);
ctx.body = 'success';
return;
}
ctx.throw(400, 'Invalid app');
} catch (e) {
ctx.throw(400, e.message);
}
})
.addTo(app);

View File

@@ -0,0 +1,84 @@
import { sequelize } from '@/modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
export type MicroApp = Partial<InstanceType<typeof MicroAppModel>>;
type MicroAppData = {
file?: {
path: string;
size: number;
hash: string;
};
data?: any;
collection?: any;
};
export class MicroAppModel extends Model {
declare id: string;
declare title: string;
declare description: string;
declare type: string;
declare tags: string[];
declare data: MicroAppData;
declare uid: string;
declare updatedAt: Date;
declare createdAt: Date;
declare source: string;
declare share: boolean;
declare uname: string;
}
MicroAppModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
comment: 'id',
},
title: {
type: DataTypes.STRING,
defaultValue: '',
},
description: {
type: DataTypes.STRING,
defaultValue: '',
},
tags: {
type: DataTypes.JSONB,
defaultValue: [],
},
type: {
type: DataTypes.STRING,
defaultValue: '',
},
source: {
type: DataTypes.STRING,
defaultValue: '',
},
data: {
type: DataTypes.JSONB,
defaultValue: {},
},
share: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
uname: {
type: DataTypes.STRING,
defaultValue: '',
},
uid: {
type: DataTypes.UUID,
allowNull: true,
},
},
{
sequelize,
tableName: 'micro_apps',
// paranoid: true,
},
);
MicroAppModel.sync({ alter: true, logging: false }).catch((e) => {
console.error('MicroAppModel sync', e);
});

View File

@@ -0,0 +1,60 @@
import { minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { checkFileExistsSync } from '@/routes/page/module/cache-file.ts';
import { useFileStore } from '@abearxiong/use-file-store';
import fs from 'fs';
import path from 'path';
import * as tar from 'tar';
const appsPath = useFileStore('apps', { needExists: true });
export type InstallAppOpts = {
path?: string;
key?: string;
};
export const appCheck = async (opts: InstallAppOpts) => {
const { key } = opts;
const directory = path.join(appsPath, key);
if (checkFileExistsSync(directory)) {
return true;
}
return false;
};
export const installApp = async (opts: InstallAppOpts) => {
const { key } = opts;
const fileStream = await minioClient.getObject(bucketName, opts.path);
const pathName = opts.path.split('/').pop();
const directory = path.join(appsPath, key);
if (!checkFileExistsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
const filePath = path.join(directory, pathName);
const writeStream = fs.createWriteStream(filePath);
fileStream.pipe(writeStream);
await new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
// 解压 tgz文件
const extractPath = path.join(directory);
await tar.x({
file: filePath,
cwd: extractPath,
});
const pkgs = path.join(extractPath, 'package.json');
if (!checkFileExistsSync(pkgs)) {
throw new Error('Invalid package.json');
}
const json = fs.readFileSync(pkgs, 'utf-8');
const pkg = JSON.parse(json);
const { name, version, app } = pkg;
if (!name || !version || !app) {
throw new Error('Invalid package.json');
}
app.key = key;
fs.writeFileSync(pkgs, JSON.stringify(pkg, null, 2));
// fs.unlinkSync(filePath);
return { path: filePath };
};

View File

@@ -0,0 +1,29 @@
import { checkFileExistsSync } from '@/routes/page/module/cache-file.ts';
import { useFileStore } from '@abearxiong/use-file-store';
import fs from 'fs';
import path from 'path';
const appsPath = useFileStore('apps', { needExists: true });
export const loadApp = async (key: string) => {
const directory = path.join(appsPath, key);
if (!checkFileExistsSync(directory)) {
throw new Error('app not found');
}
const pkgs = path.join(directory, 'package.json');
if (!checkFileExistsSync(pkgs)) {
throw new Error('Invalid package.json');
}
const json = fs.readFileSync(pkgs, 'utf-8');
const pkg = JSON.parse(json);
const { name, version, app } = pkg;
if (!name || !version || !app) {
throw new Error('Invalid package.json');
}
const mainEntry = path.join(directory, app.entry);
if (!checkFileExistsSync(mainEntry)) {
throw new Error('Invalid main entry');
}
const main = await import(mainEntry);
return main;
};