Skip to content

Commit

Permalink
feat(cloud-func): support unpublished func debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 1, 2021
1 parent 83877a7 commit 61b8f9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
19 changes: 9 additions & 10 deletions packages/app-service/src/router/function/debug.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-10-06 21:37:48
* @LastEditTime: 2021-11-01 18:55:44
* @Description:
*/

import { Request, Response } from 'express'
import { FunctionContext, CloudFunction } from 'cloud-function-engine'
import { getFunctionByName } from '../../api/function'
import { parseToken } from '../../lib/utils/token'
import { logger } from '../../lib/logger'
import { addFunctionLog } from '../../api/function-log'
Expand All @@ -20,27 +19,27 @@ export async function handleDebugFunction(req: Request, res: Response) {
const requestId = req['requestId']
const func_name = req.params?.name
const debug_token = req.get('debug-token') ?? undefined
const func_data = req.body?.func
const param = req.body?.param

if (!func_data) {
return res.send({ code: 1, error: 'function data not found', requestId })
}

// verify the debug token
const parsed = parseToken(debug_token as string)
if (!parsed || parsed.type !== 'debug') {
return res.status(403).send('permission denied: invalid debug token')
}

// load function data from db
const funcData = await getFunctionByName(func_name)
if (!funcData) {
return res.send({ code: 1, error: 'function not found', requestId })
}

const func = new CloudFunction(funcData)
const func = new CloudFunction(func_data)

try {
// execute the func
const ctx: FunctionContext = {
query: req.query,
files: req.files as any,
body: req.body,
body: param,
headers: req.headers,
method: req.method,
auth: req['auth'],
Expand Down
9 changes: 6 additions & 3 deletions packages/system-client/src/api/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@ export function publishFunctions() {
/**
* Debug cloud function
*/
export async function launchFunction(functionName, data, debug = false) {
export async function launchFunction(func, param, debug = false) {
const appid = store.state.app.appid
const res = await axios({
url: process.env.VUE_APP_BASE_API_APP + `/${appid}/func/debug/${functionName}`,
url: process.env.VUE_APP_BASE_API_APP + `/${appid}/func/debug/${func.name}`,
method: 'post',
data: data,
data: {
func,
param
},
headers: {
'debug-token': debug
}
Expand Down
10 changes: 6 additions & 4 deletions packages/system-client/src/views/cloudfunction/debug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ import FunctionLogDetail from './components/FunctionLogDetail'
import FunctionEditor from '@/components/FunctionEditor'
import jsonEditor from '@/components/JsonEditor/param'
import { getFunctionById, getFunctionLogs, launchFunction, updateFunctionCode } from '../../api/func'
import { publishFunctions } from '../../api/func'
import { showError, showSuccess } from '@/utils/show'
import { debounce } from 'lodash'
Expand Down Expand Up @@ -217,8 +216,12 @@ export default {
if (r.error) { return showError('保存失败!') }
this.func = r.data
this.value = this.func.code
this.invokeParams = this.parseInvokeParam(this.func.debugParams) ?? defaultParamValue
if (showTip) {
await this.getFunction()
// await this.getFunction()
showSuccess('已保存: ' + this.func.name)
}
},
Expand All @@ -231,9 +234,8 @@ export default {
if (this.loading) return
this.loading = true
await publishFunctions(this.appid)
const param = this.parseInvokeParam(this.invokeParams)
const res = await launchFunction(this.func.name, param, debug_token)
const res = await launchFunction(this.func, param, debug_token)
.finally(() => { this.loading = false })
this.invokeRequestId = res.headers['requestid']
Expand Down

0 comments on commit 61b8f9d

Please sign in to comment.