Skip to content

Commit

Permalink
feat: 实现云函数与触发器自动部署到 app db;修改文档;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jul 29, 2021
1 parent 0a8ef9e commit 3ea4713
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
11 changes: 4 additions & 7 deletions packages/devops-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@ npm start

- 【已完成】 部署数据访问策略:写入 app db __deployed__rules, app server 应监听该库之变化(watch)
- 【已完成】 部署云函数:写入 app db __deployed__functions, app-server 运行前直接读取即可
- ** 调试云函数:调用 app server 提供的调试接口,由 devops server 转发,或者发调试令牌直接调
- ** 部署应用触发器(新增、修改),应监听该库之变化
- 重新考虑云函数调用的问题: devops client 调用需要知道其地址,并且云函数的日志当前只能写入 app db,导致 devops client 无法获取日志

- ** 思考云函数名称是否唯一,是否可修改,是否支持名字空间
- ** 思考触发器如何存储,是否存云函数文档中
- 【已完成】调试云函数:调用 app server 提供的调试接口,由 devops server 转发,或者发调试令牌直接调
- 【已完成】 部署应用触发器(新增、修改),应监听该库之变化

- 【已完成】考虑使用 mongo watch() 替代 less-api accessor 的数据事件,应用于部署监听和云函数事件(可获取变更数据的完整信息)
- 【已完成】数据管理-集合管理:使用 devops server dbm entry,可具备完整的 app db 管理能力
- 【已完成】数据管理-集合管理:使用 devo ps server dbm entry,可具备完整的 app db 管理能力
- 【已完成】将 devops 中表名修改,增加前缀, 如: __admins,以适应用户可能用同一数据库,跑 app & devops server;

- 实现远程部署推送:远程推送源管理,推送云函数(及触发器),推送访问规则
- 远程部署请求管理:查询收到的部署请求,可拒绝,可接受

- 考虑以后去除 app server 中的 RBAC admin 相关的代码,转由云函数实现,云函数可初始配置 应用的 $injections getter
- 或将 app server 中的 admin entry 移至内置云函数中实现

5 changes: 5 additions & 0 deletions packages/devops-server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const Constants = {
*/
function_collection: '__deployed__functions',

/**
* 部署到 app db 中云函数触发器集合名
*/
trigger_collection: '__deployed__triggers',

/**
* 部署到 app db 中的访问策略集合名
*/
Expand Down
46 changes: 31 additions & 15 deletions packages/devops-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
import * as express from 'express'
import { parseToken } from './lib/utils/token'
import { parseToken, splitBearerToken } from './lib/utils/token'
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()
server.use(express.json())

// 服务端开放跨域
server.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type')
res.header('Access-Control-Allow-Methods', '*')
req['requestId'] = uuidv4()
next()
})

// 解析 Bearer Token
server.use(function (req, _res, next) {
const bearer = req.headers['authorization'] ?? ''
const splitted = bearer.split(' ')
const token = splitted.length === 2 ? splitted[1] : ''
const token = splitBearerToken(req.headers['authorization'] ?? '')
const auth = parseToken(token) || null
req['auth'] = auth

const requestId = req['requestId'] || uuidv4()
const requestId = req['requestId'] = uuidv4()
logger.info(`[${requestId}] ${req.path} start request`)
logger.debug(`[${requestId}] auth: ` + JSON.stringify(auth))
next()
})

server.use(router)

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




/**
* 监听云函数、触发器变化 & 部署
*/
{
const acc = Globals.sys_accessor
acc.ready.then(() => {
acc.db
.watch([], { fullDocument: 'updateLookup' })
.on('change', doc => {
const coll = doc.ns.coll
if(coll === '__functions') {
deployFunctions()
}

if(coll === '__triggers') {
deployTriggers()
}
})
})
}

13 changes: 10 additions & 3 deletions packages/devops-server/src/lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ export class Globals {
}

/**
* 初始化全局对象
* 应用初始化
*/
static init() {
this._initGlobalObjects()

Object.freeze(Globals)
}

/**
* 初始化全局对象
*/
private static _initGlobalObjects() {
// 创建全局日志对象
if (null === this._logger) {
this._logger = createLogger('server')
Expand All @@ -67,8 +76,6 @@ export class Globals {
if (null === this._app_db) {
this._app_db = this.createAppDb()
}

Object.freeze(Globals)
}

/**
Expand Down
5 changes: 0 additions & 5 deletions packages/devops-server/src/router/deploy/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import * as express from 'express'
import { checkPermission } from '../../api/permission'
import { deployAccessPolicy } from '../../api/rules'
import { Globals } from '../../lib/globals'

const logger = Globals.logger
export const DeployRouter = express.Router()


/**
*
*/
DeployRouter.post('/policy', async (req, res) => {
const requestId = req['requestId']
logger.info(requestId, `post /deploy/policy`)

// 权限验证
const code = await checkPermission(req['auth']?.uid, 'deploy.policy')
Expand Down

0 comments on commit 3ea4713

Please sign in to comment.