Skip to content

Commit

Permalink
feat: 新增发布函数、触发器接口;取消 watch 监听发布;支持发布时编译云函数;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 30, 2021
1 parent e0fab5b commit ba2e538
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 80 deletions.
8 changes: 4 additions & 4 deletions packages/devops-server/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const { permissions } = require('./sys-permissions')
const { FunctionLoader } = require('./func-loader')
const { Constants } = require('../dist/constants')
const { Globals } = require('../dist/lib/globals')
const { deployFunctions } = require('../dist/api/function')
const { deployAccessPolicy } = require('../dist/api/rules')
const { publishFunctions } = require('../dist/api/function')
const { publishAccessPolicy } = require('../dist/api/rules')

const sys_accessor = Globals.sys_accessor

Expand Down Expand Up @@ -44,10 +44,10 @@ async function main() {
await createBuiltinFunctions()

// 部署访问策略
await deployAccessPolicy().then(() => console.log('policy deployed'))
await publishAccessPolicy().then(() => console.log('policy deployed'))

// 部署云函数
await deployFunctions().then(() => console.log('functions deployed'))
await publishFunctions().then(() => console.log('functions deployed'))

sys_accessor.close()
app_accessor.close()
Expand Down
9 changes: 3 additions & 6 deletions packages/devops-server/init/sys-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ exports.permissions = [
{ name: 'rule.read', label: '获取访问规则' },
{ name: 'rule.edit', label: '编辑访问规则' },
{ name: 'rule.delete', label: '删除访问规则' },
{ name: 'rule.apply', label: '部署访问规则' },

{ name: 'function.create', label: '创建云函数' },
{ name: 'function.read', label: '获取云函数' },
{ name: 'function.edit', label: '编辑云函数' },
{ name: 'function.delete', label: '删除云函数' },
{ name: 'function.publish', label: '发布云函数' },
{ name: 'function.debug', label: '调试云函数' },

{ name: 'function_logs.read', label: '查看云函数日志' },
Expand All @@ -35,14 +33,13 @@ exports.permissions = [
{ name: 'trigger.read', label: '获取触发器' },
{ name: 'trigger.edit', label: '编辑访触发器' },
{ name: 'trigger.delete', label: '删除触发器' },
{ name: 'trigger.apply', label: '应用触发器' },

{ name: 'database.manage', label: '数据库数据管理'},
{ name: 'collections.get', label: 'DBM-获取集合列表'},
{ name: 'collections.createIndex', label: 'DBM-创建集合索引'},
{ name: 'collections.deleteIndex', label: 'DBM-删除集合索引' },


{ name: 'deploy.policy', label: '部署数据访问策略'},
{ name: 'deploy.function', label: '部署云函数'}
{ name: 'publish.policy', label: '发布数据访问策略'},
{ name: 'publish.function', label: '发布云函数' },
{ name: 'publish.trigger', label: '发布触发器' },
]
3 changes: 2 additions & 1 deletion packages/devops-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"log4js": "^6.3.0",
"mongodb": "^4.0.1",
"uuid": "^8.3.2",
"validator": "^12.2.0"
"validator": "^12.2.0",
"cloud-function-engine": "^0.0.5"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
Expand Down
23 changes: 19 additions & 4 deletions packages/devops-server/src/api/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { Constants } from "../constants"
import { Globals } from "../lib/globals"
import { compileTs2js } from 'cloud-function-engine/dist/utils'

const db = Globals.sys_db

Expand Down Expand Up @@ -39,28 +40,42 @@ export async function getFunctionById(func_id: string) {
return r.data
}



/**
* 部署云函数
* 发布云函数
* 实为将 sys db __functions 集合,复制其数据至 app db 中
*/
export async function deployFunctions() {
export async function publishFunctions() {
const logger = Globals.logger

const app_accessor = Globals.app_accessor
const ret = await Globals.sys_accessor.db.collection('__functions').find().toArray()

// compile
const data = ret.map(fn => compileFunction(fn))

const session = app_accessor.conn.startSession()

try {
await session.withTransaction(async () => {
const _db = app_accessor.db
const app_coll = _db.collection(Constants.function_collection)
await app_coll.deleteMany({})
await app_coll.insertMany(ret)
await app_coll.insertMany(data)
})
} catch (error) {
logger.error(error)
} finally {
await session.endSession()
}
}
}

/**
* 编译函数
* @param func
*/
function compileFunction(func: any) {
func.compiledCode = compileTs2js(func.code)
return func
}
4 changes: 2 additions & 2 deletions packages/devops-server/src/api/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export async function getAccessPolicy(category: string): Promise<any> {
}

/**
* 部署访问策略
* 发布访问策略
* 实为将 sys_db.__rules 中的表,复制其数据至 app_db 中
*/
export async function deployAccessPolicy() {
export async function publishAccessPolicy() {
const logger = Globals.logger

const app_accessor = Globals.app_accessor
Expand Down
4 changes: 2 additions & 2 deletions packages/devops-server/src/api/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export async function getTriggerById(id: string) {


/**
* 部署触发器
* 发布触发器
* 实为将 sys db __triggers 集合,复制其数据至 app db 中
*/
export async function deployTriggers() {
export async function publishTriggers() {
const logger = Globals.logger

const app_accessor = Globals.app_accessor
Expand Down
29 changes: 1 addition & 28 deletions packages/devops-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { v4 as uuidv4 } from 'uuid'
import Config from './config'
import { router } from './router/index'
import { Globals } from './lib/globals'
import { deployFunctions } from './api/function'
import { deployTriggers } from './api/trigger'

const logger = Globals.logger
const server = express()
Expand All @@ -25,29 +23,4 @@ server.use(function (req, _res, next) {

server.use(router)

server.listen(Config.PORT, () => console.log(`listened on ${Config.PORT}`))




/**
* 监听云函数、触发器变化 & 部署
*/
{
const acc = Globals.sys_accessor
acc.ready.then(() => {
acc.db
.watch()
.on('change', doc => {
const coll = doc.ns.coll
if (coll === '__functions') {
// TODO 此处调试时有问题,客户端保存后,函数被清除,无法获取
deployFunctions().then(() => logger.debug('__functions changed: deployed'))
}

if (coll === '__triggers') {
deployTriggers().then(() => logger.debug('__triggers changed: deployed'))
}
})
})
}
server.listen(Config.PORT, () => console.log(`listened on ${Config.PORT}`))
31 changes: 0 additions & 31 deletions packages/devops-server/src/router/deploy/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/devops-server/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Router } from 'express'
import { AdminRouter } from './admin/index'
import { DevOpsEntryRouter } from './entry'
import { DbmRouter } from './dbm'
import { DeployRouter } from './deploy'
import { PublishRouter } from './publish'

export const router = Router()

router.use('/admin', DevOpsEntryRouter)
router.use('/admin', AdminRouter)
router.use('/dbm', DbmRouter)
router.use('/deploy', DeployRouter)
router.use('/publish', PublishRouter)
86 changes: 86 additions & 0 deletions packages/devops-server/src/router/publish/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as express from 'express'
import { publishFunctions } from '../../api/function'
import { checkPermission } from '../../api/permission'
import { publishAccessPolicy } from '../../api/rules'
import { publishTriggers } from '../../api/trigger'

export const PublishRouter = express.Router()

/**
* 发布访问策略
*/
PublishRouter.post('/policy', async (req, res) => {

// 权限验证
const code = await checkPermission(req['auth']?.uid, 'publish.policy')
if (code) {
return res.status(code).send()
}

try {
const r = await publishAccessPolicy()

return res.send({
code: 0,
data: r
})
} catch (error) {
return res.send({
code: 1,
error: error
})
}
})

/**
* 发布云函数
*/
PublishRouter.post('/functions', async (req, res) => {

// 权限验证
const code = await checkPermission(req['auth']?.uid, 'publish.function')
if (code) {
return res.status(code).send()
}

try {
const r = await publishFunctions()

return res.send({
code: 0,
data: r
})
} catch (error) {
return res.send({
code: 1,
error: error
})
}
})


/**
* 发布触发器
*/
PublishRouter.post('/triggers', async (req, res) => {

// 权限验证
const code = await checkPermission(req['auth']?.uid, 'publish.trigger')
if (code) {
return res.status(code).send()
}

try {
const r = await publishTriggers()

return res.send({
code: 0,
data: r
})
} catch (error) {
return res.send({
code: 1,
error: error
})
}
})

0 comments on commit ba2e538

Please sign in to comment.