飞布产品手册
官网B站Github
V2.0
V2.0
  • 序言
  • 更新日志
    • 更新日志V2.0
    • V2.0更新说明
    • 更新日志V1.0
  • 产品简介
    • 什么是飞布?
    • 飞布的价值
    • 飞布的优势
    • 应用场景
    • 数据安全
    • 产品案例
  • 快速入门
    • 初识飞布
    • 快速上手
      • 图文版
    • 词汇概览
    • 工作原理
  • 基础-可视化开发
    • 概览
      • CLI
      • 控制台
        • 主功能区
    • 数据源
      • 数据库
        • 数据库连接
          • 高级设置
        • 数据建模
        • 数据预览
      • Prisma 数据源
      • REST 数据源
      • GraphQL 数据源
      • 消息队列
    • API构建
      • 可视化构建
        • API规范
      • 批量新建
      • HTTP请求流程指令
      • 使用API
      • 实时查询
      • 实时推送
      • 关联查询
      • 数据缓存
      • 常见用例
    • 身份验证
      • 授权码模式
        • 身份验证(废弃)
      • 隐式模式
      • 数据权限控制
    • 身份授权
      • RBAC
        • 授权与访问控制(废弃)
      • 接口权限控制
      • 开放API
    • 文件存储
      • S3配置及使用
      • 文件管理面板
      • 高级配置:profile
  • 进阶-钩子机制
    • 钩子概览
    • 启动钩子
      • Node钩子
      • Golang钩子
      • Java钩子
      • Python钩子
    • OPERATION钩子
    • 身份验证钩子
    • graphql钩子
    • 函数钩子
      • function
      • proxy
    • 文件上传钩子
    • 内部调用
  • 使用-部署上线
    • 部署运维
      • 手动部署
        • 流水线部署(废弃)
      • Docker部署
      • 飞布云
      • sealos部署
    • 接口安全
      • CSRF token 保护
      • 跨域访问
    • 客户端SDK
      • Js SDK
      • 微信小程序SDK
      • Flutter SDK
      • uniapp SDK
    • 性能测试
  • 迁移到V2
  • 环境准备
    • 文件存储 S3
    • 身份认证 OIDC
    • NodeJs环境
  • 实战案例
    • Amis Admin
      • 管理后台-refine(废弃)
    • 实时TODO LIST
    • 语音版ChatGPT
    • AI魔法师实战
    • 阿里低代码引擎
    • appsmith集成
  • Roadmap
  • 常见问题
  • 核心概念
    • GraphQL
    • 超图
    • 请求时序图
    • 服务端Operation
  • 二次开发
    • 钩子规范
      • 钩子规范bak
    • 模板规范
    • 自定义模板
    • 其它参考
由 GitBook 提供支持
在本页

这有帮助吗?

在GitHub上编辑
  1. 进阶-钩子机制
  2. 函数钩子

function

function钩子注册到Fireboom中为一个API,且有出入参定义(json类型),此外还支持实时查询和权限控制。

新建function

1,在 Fireboom 控制台点击数据源面板的+号,进入数据源新建页。

2,在数据源新建页面,选择 脚本-> Function,设置名称为:fun1。

3,系统自动初始化如下脚本。

custom-go/function/fun1.go
package function

import (
	"custom-go/pkg/base"
	"custom-go/pkg/plugins"
	"custom-go/pkg/wgpb"
)

func init() {
	// 注册 function
	plugins.RegisterFunction[helloReq, helloRes](hello, wgpb.OperationType_MUTATION)
}

type helloReq struct {
	Username string    `json:"username"`
	Password string    `json:"password"`
	Info     helloInfo `json:"info,omitempty"`
}

type helloInfo struct {
	Code    string `json:"code,omitempty"`
	Captcha string `json:"captcha,omitempty"`
}

type helloRes struct {
	Msg  string `json:"msg"`
	Data string `json:"data"`
}

func hello(hook *base.HookRequest, body *base.OperationBody[helloReq, helloRes]) (*base.OperationBody[helloReq, helloRes], error) {
	if body.Input.Username != "John" || body.Input.Password != "123456" {
		body.Response = &base.OperationBodyResponse[helloRes]{
			Errors: []base.GraphQLError{{Message: "username or password wrong"}},
		}
		return body, nil
	}

	body.Response = &base.OperationBodyResponse[helloRes]{Data: helloRes{Msg: "hello success"}}
	return body, nil
}

默认填充的是示例代码,你可以根据业务需求修改代码。

4,在main.go中匿名引入该包,然后重新启动钩子服务

custom-go/main.go
package main

import (
	// _ "custom-go/customize"
	// 匿名引入该库
	 _ "custom-go/function"
	// _ "custom-go/proxy"
	"custom-go/server"
)

func main() {
	server.Execute()
}

5,钩子服务启动后,生成对应的配置文件custom-x/function/fun1.json

function          
├─ fun1.go   # function 钩子代码   
└─ fun1.json # 该钩子对应的配置文件
fun1.json
{
    "name": "fun1",
    // 请求类型,1代表POST请求
    "operationType": 1,
    // 请求入参的JSON SCHEMA
    "variablesSchema": "{\"definitions\":{\"helloInfo\":{\"properties\":{\"captcha\":{\"type\":\"string\"},\"code\":{\"type\":\"string\"}},\"type\":\"object\"}},\"properties\":{\"info\":{\"$ref\":\"#/definitions/helloInfo\"},\"password\":{\"type\":\"string\"},\"username\":{\"type\":\"string\"}},\"required\":[\"username\",\"password\"],\"type\":\"object\"}",
    // 响应结果的JSON SCHEMA
    "responseSchema": "{\"properties\":{\"data\":{\"type\":\"string\"},\"msg\":{\"type\":\"string\"}},\"required\":[\"msg\",\"data\"],\"type\":\"object\"}",
    "cacheConfig": null,
    // 授权配置
    "authenticationConfig": null,
    // 实时查询配置,仅限于GET请求
    "liveQueryConfig": null,
    // RBAC配置
    "authorizationConfig": null,
    "hooksConfiguration": null,
    "variablesConfiguration": null,
    "engine": 0,
    "path": "/function/fun1", // 路由
}

6,Fireboom服务每秒触发1次健康检查,将获取如下结果:

{
    "report": {
        // function 钩子
        "functions": [
            "fun1"
        ],
        "time": "2023-09-06T17:18:21.957519+08:00"
    },
    // 钩子服务的状态
    "status": "ok"
}

可以看到 report.functions 中包含fun1接口。接着读取步骤5中的json配置,将该function注册到API列表中,类型为function。

请访问路由如下:

curl http://localhost:9991/operations/function/fun1
-X POST
-H 'Content-Type: application/json'
--data-raw '{"info":{"captcha":"string","code":"string"},"password":"string","username":"string"}'
--compressed

其路由规则为:

http://localhost:9991/operations/function/fun1

如果report没有变化,则不会触发编译!

7,最后,Fireboom发送通知,触发控制台更新,在API管理面板展示API: fun1 。

8,前往swagger文档可测试API:fun1

上一页函数钩子下一页proxy

最后更新于1年前

这有帮助吗?