import { app } from '@/app.ts'; import { MicroAppModel } from './models.ts'; import { appPathCheck, installApp } from './module/install-app.ts'; import { loadApp } from './module/load-app.ts'; import { manager } from './manager-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 // 把对应的应用安装到系统的apps目录下,并解压,然后把配置项写入数据库配置 // key 是应用的唯一标识,和package.json中的key一致,绑定关系 // 安装就就一定存在状态。这个时候需要判断这个应用的依赖关系,这个应用的开启状态。 // 修改app的类型,这些模块是存在添加,不存在则删除。 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 appPathCheck({ key }); if (check) { ctx.throw(400, 'App already exists, please remove it first'); } const installAppData = await installApp({ path, key }); await manager.add(installAppData.showAppInfo); ctx.body = installAppData; }) .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); // curl http://localhost:4002/api/router?path=micro-app&key=unload app .route({ path: 'micro-app', key: 'unload', }) .define(async (ctx) => { // const { key } = ctx.query?.data; const key = 'mark'; const main = manager.getAppShowInfo(key); if (!main) { ctx.throw(400, 'Invalid app'); } manager.removeApp(key); ctx.body = main; }) .addTo(app);