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中匿名引入该包,然后重新启动钩子服务

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

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

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

请访问路由如下:

其路由规则为:

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

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

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

最后更新于

这有帮助吗?