Skip to content

Commit

Permalink
feat(sys-server): add application template init support;
Browse files Browse the repository at this point in the history
  • Loading branch information
sulnong committed Feb 5, 2022
1 parent d66c6c9 commit 1d96659
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
13 changes: 7 additions & 6 deletions packages/system-server/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2022-01-19 15:57:36
* @Description:
* @Description:
*/

import { deepFreeze } from "../utils/lang"
import { permissions } from "./permissions"
import { roles } from "./roles"
import { deepFreeze } from '../utils/lang'
import { permissions } from './permissions'
import { roles } from './roles'

const coll_prefix = 'sys_'

Expand Down Expand Up @@ -52,6 +52,7 @@ export const Constants = {
deploy_requests: coll_prefix + 'deploy_requests',
applications: coll_prefix + 'applications',
recycles: coll_prefix + 'recycles',
app_templates: coll_prefix + 'app_templates',
},

/**
Expand All @@ -64,7 +65,7 @@ export const Constants = {
*/
roles: roles,

SYSTEM_EXTENSION_APPID: `00000000-0000-0000-0000-000000000000`
SYSTEM_EXTENSION_APPID: `00000000-0000-0000-0000-000000000000`,
}

deepFreeze(Constants)
deepFreeze(Constants)
56 changes: 49 additions & 7 deletions packages/system-server/src/router/application/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-09 14:46:44
* @LastEditTime: 2021-12-21 19:10:02
* @Description:
* @Description:
*/

import fsp = require('fs/promises')
import { Request, Response } from 'express'
import { getApplicationByAppid, publishApplicationPackages } from '../../api/application'
import {
getApplicationByAppid,
publishApplicationPackages,
} from '../../api/application'
import { publishFunctions } from '../../api/function'
import { checkPermission } from '../../api/permission'
import { publishAccessPolicies } from '../../api/policy'
import { Constants } from '../../constants'
import { ApplicationImporter } from '../../lib/importer'
import { logger } from '../../lib/logger'
import { DatabaseAgent } from '../../lib/db-agent'
import { Binary, ObjectId } from 'mongodb'

const { APPLICATION_ADD } = Constants.permissions

/**
* The handler of import application by id
* @param req
* @param res
* @returns
* @param req
* @param res
* @returns
*/
export async function handleImportApplication(req: Request, res: Response) {
const uid = req['auth']?.uid
Expand All @@ -35,7 +40,6 @@ export async function handleImportApplication(req: Request, res: Response) {
const code = await checkPermission(uid, APPLICATION_ADD.name, app)
if (code) return res.status(code).send()


try {
const data = await fsp.readFile(file.path)
const importer = new ApplicationImporter(app, data)
Expand All @@ -51,4 +55,42 @@ export async function handleImportApplication(req: Request, res: Response) {
logger.error('import application got error:', error)
return res.status(400).send({ error: error })
}
}
}

export async function handleInitApplicationWithTemplate(
req: Request,
res: Response
) {
const uid = req['auth']?.uid
const appid = req.params.appid
const template_id = req.body?.template_id
if (!template_id) return res.status(422).send('template_id missing')

const app = await getApplicationByAppid(appid)
if (!app) return res.status(422).send('invalid appid')

// check permission
const code = await checkPermission(uid, APPLICATION_ADD.name, app)
if (code) return res.status(code).send()

const coll = DatabaseAgent.db.collection(Constants.cn.app_templates)
try {
const template = await coll.findOne({
_id: new ObjectId(req.body?.template_id),
})
if (!template) return res.status(422).send('invalid template_id')

const bin = template.package?.data as Binary
const importer = new ApplicationImporter(app, bin.buffer)
importer.parse()
await importer.import()

await publishFunctions(app)
await publishAccessPolicies(app)
await publishApplicationPackages(app.appid)
return res.send({ data: 'ok' })
} catch (error) {
logger.error('import application got error:', error)
return res.status(400).send({ error: error })
}
}
12 changes: 8 additions & 4 deletions packages/system-server/src/router/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-29 11:35:11
* @LastEditTime: 2022-01-19 14:47:05
* @Description:
* @Description:
*/

import { Router } from 'express'
Expand All @@ -14,7 +14,7 @@ import { handleGetCollaborators, handleGetRoles, handleInviteCollaborator, handl
import { handleCreateApplication } from './create'
import { handleExportApplication } from './exporter'
import { handleGetApplicationByAppid, handleMyApplications } from './get'
import { handleImportApplication } from './importer'
import { handleImportApplication, handleInitApplicationWithTemplate } from './importer'
import { handleAddPackage, handleGetPackages, handleRemovePackage, handleUpdatePackage } from './packages'
import { handleRemoveApplication } from './remove'
import { handleStopApplicationService, handleStartApplicationService } from './service'
Expand All @@ -35,7 +35,7 @@ const uploader = multer({
export const ApplicationRouter = Router()

/**
* Get my applications
* Get my applications
*/
ApplicationRouter.get('/my', handleMyApplications)

Expand Down Expand Up @@ -65,7 +65,7 @@ ApplicationRouter.post('/:appid/service/start', handleStartApplicationService)
ApplicationRouter.post('/:appid/service/stop', handleStopApplicationService)

/**
* Remove an application by appid
* Remove an application by appid
*/
ApplicationRouter.delete('/:appid', handleRemoveApplication)

Expand Down Expand Up @@ -110,6 +110,10 @@ ApplicationRouter.get('/:appid/export', handleExportApplication)
*/
ApplicationRouter.post('/:appid/import', uploader.single('file'), handleImportApplication)

/**
* Init appliaction with template
*/
ApplicationRouter.post('/:appid/init-with-template', handleInitApplicationWithTemplate)

/**
* Get packages from app
Expand Down

0 comments on commit 1d96659

Please sign in to comment.