Skip to content

Commit

Permalink
feat: 支持云函数调试请求令牌
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 4, 2021
1 parent ac237ef commit 57dedc9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 13 additions & 6 deletions packages/app-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export default class Config {
* 获取数据库连接配置
*/
static get db() {
if(!process.env['DB']) {
if (!process.env['DB']) {
throw new Error('env: `DB` is missing')
}

if(!process.env['DB_URI']) {
if (!process.env['DB_URI']) {
throw new Error('env: `DB_URI` is missing')
}

Expand All @@ -29,8 +29,8 @@ export default class Config {
* 指定服务端密钥,用于生成 token
*/
static get SERVER_SECRET_SALT(): string {
const secret_salt = process.env['SERVER_SECRET_SALT'] ?? process.env['SERVER_SALT']
if(!secret_salt) {
const secret_salt = process.env['SERVER_SECRET_SALT'] ?? process.env['SERVER_SALT']
if (!secret_salt) {
throw new Error('env: `SERVER_SECRET_SALT` is missing')
}
return secret_salt
Expand All @@ -43,7 +43,7 @@ export default class Config {

// 临时文件目录
static get TMP_PATH(): string {
const tmp_path = process.env['TMP_PATH'] ?? path.join(process.cwd(), "tmp")
const tmp_path = process.env['TMP_PATH'] ?? path.join(process.cwd(), "tmp")
return tmp_path
}

Expand All @@ -57,10 +57,17 @@ export default class Config {
/**
* 指定服务监听端口号,缺省为 8000
*/
static get PORT(): number{
static get PORT(): number {
return (process.env.PORT ?? 8000) as number
}

/**
* 指定开启云函数日志: "always" | "debug" | "none"
*/
static get ENABLE_CLOUD_FUNCTION_LOG(): string {
return (process.env.ENABLE_CLOUD_FUNCTION_LOG ?? 'debug')
}

/**
* 是否生产环境
*/
Expand Down
15 changes: 9 additions & 6 deletions packages/app-server/src/router/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as uuid from 'uuid'
import { getFunctionByName } from '../../api/function'
import { Globals } from '../../lib/globals/index'
import { Constants } from '../../constants'
import { parseToken } from '../../lib/utils/token'
import Config from '../../config'

// 设置云函数中的加载函数
CloudFunction.require_func = Globals.require_func
Expand Down Expand Up @@ -48,13 +50,13 @@ async function handleInvokeFunction(req: Request, res: Response) {
return res.send({ code: 1, error: 'invalid function name', requestId })
}

const debug = req.query?.debug ?? false
const debug = req.get('debug-token') ?? undefined

// 调试权限验证: @TODO 需要通过令牌来控制调试权限
// 调试权限验证:
if (debug) {
const auth = req['auth']
if (!auth || auth.type !== 'admin') {
return res.status(403).send('permission denied')
const parsed = parseToken(debug as string)
if (!parsed || parsed.type !== 'debug') {
return res.status(403).send('permission denied: invalid debug token')
}
}

Expand Down Expand Up @@ -97,7 +99,8 @@ async function handleInvokeFunction(req: Request, res: Response) {
const result = await func.invoke(ctx)

// 将云函数调用日志存储到数据库
if (debug) {
const shouldLog = Config.ENABLE_CLOUD_FUNCTION_LOG === 'always' || (Config.ENABLE_CLOUD_FUNCTION_LOG === 'debug' && debug)
if (shouldLog) {
await db.collection(Constants.function_log_collection)
.add({
requestId: requestId,
Expand Down

0 comments on commit 57dedc9

Please sign in to comment.