webhookrouters

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddWebhookConfig

func AddWebhookConfig(c *gin.Context)

AddWebhookConfig router ------------------------------------- @Summary 配置一条新的webhook 步骤**2** @Description # 配置一条新的webhook @Description ## 说明 @Description 该接口用于配置一条新的webhook,并通过when字段或condition_id字段配置触发条件,当条件满足时会触发webhook,并将所需要的数据传给webhook url接口。 @Description ## 请求参数 @Description @Description - name:webhook名称,为空时系统会自动生成一个uuid @Description - url:webhook地址(POST请求),必填,当条件满足时会调用该url,并挂载数据到body中 @Description - active:是否激活,不传的话默认true @Description - when:条件,里面是一个json,具体的格式看/api/v1/webhook/condition接口说明,when其实就是一个condition条件。该字段和condition_id字段必须传一个。 @Description - condition_id:条件id,将条件condition配置到webhook上,该字段和when字段必须传一个。 @Description - need_node_list:需要的节点值列表,条件触发时会传参给webhook @Description ## 例1:当节点node1值等于123时,发送通知到http://localhost:8080/api/v1/webhook/example @Description ```json @Description { @Description "active": true, @Description "name":"webhook1", @Description "url": "http://localhost:8080/api/v1/webhook/example", @Description "when": { @Description "rule": { @Description "node_name": "node1", @Description "type": "eq", @Description "value": 123 @Description } @Description } @Description } @Description ``` @Description 使用when字段会创建新的条件condition,并将其配置在这个webhook上 @Description ## 例2:使用已经配置好的条件condition @Description ```json @Description { @Description "active": true, @Description "url": "http://localhost:8080/api/v1/webhook/example", @Description "condition_id": 10 @Description } @Description ``` @Description ## 常见异常 @Description - "code": 2007 代表数据重复,不能创建重复的webhook,具体重复了哪个字段,请看ConstraintName最后一个下划线后面的字段名 @Description - "code": 400 "json: cannot unmarshal string into Go struct field AddWebhookConfigRequest.condition_id of type int64" : 看下body参数,数字类型传成了字符串 @Tags Webhook @Accept json @Produce json @Param body body AddWebhookConfigRequest true "Webhook configuration" @Success 200 {object} AddWebhookConfigResponse @Router /api/v1/webhook [post]

func CreateCondition

func CreateCondition(c *gin.Context)

CreateCondition router ------------------------------------- @Summary 创建触发条件 @Description # 参数说明 @Description ## 请求参数 @Description | 参数名称 | 类型 | 必填 | 描述 | @Description | --- | --- | --- | --- | @Description | and | list中嵌套本参数 | 否 | 规则列表,逻辑与 | @Description | or | list中嵌套本参数 | 否 | 规则列表,逻辑或 | @Description | rule | Rule | 否 | 规则 | @Description ## Rule类型 定义 @Description | 字段 | 类型 | 必填 | 描述 | @Description | --- | --- | --- | --- | @Description | node_name | string | 是 | 节点名称 | @Description | type | string | 是 | 规则类型,支持eq ne gt lt all-time in not-in | @Description | value | any | 是 | 比对值 | @Description ## 参数示例1 : 当节点MyVariable大于123时触发 @Description ```json @Description { @Description "rule": { @Description "node_name": "MyVariable", @Description "type": "gt", @Description "value": 123 @Description } @Description } @Description ``` @Description ## 参数示例2 : 当节点node1等于在["abc","def"],并且节点node2等于123时触发 @Description ```json @Description { @Description "and": [ @Description { @Description "rule": { @Description "node_name": "node1", @Description "type": "in", @Description "value": [ @Description "abc", @Description "def" @Description ] @Description } @Description }, @Description { @Description "rule": { @Description "node_name": "node2", @Description "type": "eq", @Description "value": 123 @Description } @Description } @Description ] @Description } @Description ``` @Description ## 参数示例3 : 一直触发 @Description ```json @Description { @Description "rule": { @Description "type": "all-time" @Description } @Description } @Description ``` @Description *注意:Condition是嵌套类型,Condition包含and,or,rule,所以and里面可以嵌套and。。。无限嵌套* @Tags Webhook @Accept json @Produce json @Param body body CreateConditionRequest true "创建条件" @Success 200 {object} CreateConditionResponse @Router /api/v1/webhook/condition [post]

func DalAddWebhookConfig

func DalAddWebhookConfig(req *AddWebhookConfigRequest) (*model.WebHook, *model.WebHookCondition, []model.NeedNode)

{"name":"webhook1","url":"http://localhost:8080/api/v1/webhook/example","active":true,"when":{"and":null,"or":null,"rule":{"type":"eq","node_name":"node1","value":"123"}},"condition_id":null} {"name":"webhook1","url":"http://localhost:8080/api/v1/webhook/example","active":true,"when":{"and":null,"or":null,"rule":{"type":"eq","node_name":"node1","value":"123"}},"condition_id":null}

func DalCreateCondition

func DalCreateCondition(req *CreateConditionRequest) *model.WebHookCondition

func DalDeleteCondition

func DalDeleteCondition(id int64)

func DalGetAllConditionsByPage

func DalGetAllConditionsByPage(page int, pageSize int) ([]*model.WebHookCondition, int64, error)

func DalGetConditionById

func DalGetConditionById(id int64) *model.WebHookCondition

func DalGetNeedNodesByWebhookId

func DalGetNeedNodesByWebhookId(id int64) []*model.NeedNode

func DalGetWebhookConfigById

func DalGetWebhookConfigById(id int64) (*model.WebHook, *model.WebHookCondition)

func DalGetWebhookConfigByName

func DalGetWebhookConfigByName(name string) *model.WebHook

func DalUpdateCondition

func DalUpdateCondition(id int64, req *UpdateConditionRequest) *model.WebHookCondition

func DeleteCondition

func DeleteCondition(c *gin.Context)

DeleteCondition router - @Summary 删除触发条件 @Tags Webhook @Accept json @Produce json @Param id path int true "条件ID" @Success 200 {object} DeleteConditionResponse @Router /api/v1/webhook/condition/{id} [delete]

func GetAllConditionsByPage

func GetAllConditionsByPage(c *gin.Context)

GetAllConditionsByPage router - @Summary 获取触发条件列表 @Description # 参数说明 @Description ## 请求参数 @Description | 参数名称 | 类型 | 必填 | 描述 | @Description | --- | --- | --- | --- | @Description | page | int | 是 | 页码 | @Description | page_size | int | 是 | 每页数量 | @Description ## 返回参数 @Description | 参数名称 | 类型 | 描述 | @Description | --- | --- | --- | @Description | id | int | 条件ID | @Description | condition | string | 条件表达式 | @Description | created_at | time.Time | 创建时间 | @Description | updated_at | time.Time | 更新时间 | @Tags Webhook @Accept json @Produce json @Param name query GetAllConditionsByPageRequest true "获取条件列表" @Success 200 {object} GetAllConditionsByPageResponse @Router /api/v1/webhook/conditions [get]

func GetConditionById

func GetConditionById(c *gin.Context)

func GetNeedeNodesListByWebhookId

func GetNeedeNodesListByWebhookId(id int64) []string

func GetWebhookConfigById

func GetWebhookConfigById(c *gin.Context)

GetWebhookConfigById router ------------------------------- @Summary 根据id获取webhook配置 @Description 根据id获取webhook配置 @Tags Webhook @Accept json @Produce json @Param id path string true "webhook id" @Success 200 {object} GetWebhookConfigByIdResponse @Router /api/v1/webhook/{id} [get]

func GetWebhookConfigByName

func GetWebhookConfigByName(c *gin.Context)

GetWebhookConfigByName router ------------------------------ @Summary 根据名称获取webhook配置 @Description 根据名称获取webhook配置 @Tags Webhook @Accept json @Produce json @Param name query GetWebhookConfigByNameRequest true "webhook名称" @Success 200 {object} GetWebhookConfigByNameResponse @Router /api/v1/webhook [get]

func RegisterRoutes

func RegisterRoutes(router *gin.RouterGroup)

func ServiceDeleteCondition

func ServiceDeleteCondition(id string)

func UpdateCondition

func UpdateCondition(c *gin.Context)

UpdateCondition router - @Summary 更新触发条件 @Description # 参数说明 @Description ## 请求参数 @Description | 参数名称 | 类型 | 必填 | 描述 | @Description | --- | --- | --- | --- | @Description | id | int | 是 | 条件ID | @Description | and | list中嵌套本参数 | 否 | 规则列表,逻辑与 | @Description | or | list中嵌套本参数 | 否 | 规则列表,逻辑或 | @Description | rule | Rule | 否 | 规则 | @Description ## Rule类型 定义 @Description | 字段 | 类型 | 是否必填 | 描述 | @Description | --- | --- | --- | --- | @Description | node_name | string | 是 | 节点名称 | @Description | type | string | 是 | 规则类型,支持eq ne gt lt all-time in not-in | @Description | value | any | 是 | 比对值 | @Description ## 参数示例1 : 更新条件ID为1的条件,将节点MyVariable大于123改为小于123 @Description ```json @Description { @Description "id": 1, @Description "rule": { @Description "node_name": "MyVariable", @Description "type": "lt", @Description "value": 123 @Description } @Description } @Description ``` @Description ## 参数示例2 : 更新条件ID为2的条件,将节点node1等于在["abc","def"],并且节点node2等于123改为节点node1等于在["abc","def"],并且节点node2等于123 @Description ```json @Description { @Description "id": 2, @Description "and": [ @Description { @Description "rule": { @Description "node_name": "node1", @Description "type": "in", @Description "value": [ @Description "abc", @Description "def" @Description ] @Description } @Description }, @Description { @Description "rule": { @Description "node_name": "node2", @Description "type": "eq", @Description "value": 123 @Description } @Description } @Description ] @Description } @Description ``` @Description ## 参数示例3 : 更新条件ID为3的条件,将一直触发改为节点node1等于123 @Description ```json @Description { @Description "id": 3, @Description "rule": { @Description "node_name": "node1", @Description "type": "eq", @Description "value": 123 @Description } @Description } @Description ``` @Description *注意:Condition是嵌套类型,Condition包含and,or,rule,所以and里面可以嵌套and。。。无限嵌套* @Tags Webhook @Accept json @Produce json @Param id path int true "条件ID" @Param body body UpdateConditionRequest true "更新条件" @Success 200 {object} UpdateConditionResponse @Router /api/v1/webhook/condition/{id} [put]

func WebHookExample

func WebHookExample(c *gin.Context)

WebHookExample router --------------------------------------- @Summary webhook示例 要像这个接口一样去定义参数,带两个字段,一个是values,一个是timestamp。注意,必须是post请求。 @Description webhook示例 @Tags Webhook Example @Accept json @Produce json @Param body body WebHookExampleRequest true "Webhook example" @Success 200 {object} WebHookExampleResponse @Router /api/v1/webhook/example [POST]

Types

type AddWebhookConfigRequest

type AddWebhookConfigRequest struct {
	Name         *string               `json:"name" form:"name" example:"webhook1"`                                            // webhook名称,可以为空
	Url          string                `json:"url" form:"url" binding:"required,url" example:"http://192.168.1.1:8800/notify"` // webhook地址
	Active       *bool                 `json:"active" form:"active" example:"true"`                                            // 是否激活,不传的话默认true
	When         *globaldata.Condition `json:"when" form:"when"`                                                               // 触发条件,为空时相当于通知所有数据变化
	ConditionId  *int64                `json:"condition_id" form:"condition_id" example:"1"`                                   // 条件id,不传的话默认新增条件
	NeedNodeList []string              `json:"need_node_list" form:"need_node_list" binding:"required"`                        // 需要的节点值列表,到时候会传参给webhook

} // Todo: NeedNodeList 解除required限制,因为有时候不需要汇报数据

GetWebhookConfig router 参数定义,字段描述放在字段后面

type AddWebhookConfigResponse

type AddWebhookConfigResponse struct {
	Code    int               `json:"code" example:"200"`
	Data    WebHookConfigRead `json:"data"`
	Message string            `json:"message" example:"节点添加成功"`
}

type CreateConditionRequest

type CreateConditionRequest struct {
	And  []globaldata.Condition `json:"and" form:"and"`   // 规则列表,逻辑与
	Or   []globaldata.Condition `json:"or" form:"or"`     // 规则列表,逻辑或
	Rule *globaldata.Rule       `json:"rule" form:"rule"` // 规则
}

type CreateConditionResponse

type CreateConditionResponse struct {
	Code    int                  `json:"code" example:"200"`
	Data    WebHookConditionRead `json:"data" `
	Message string               `json:"message" example:"Condition created successfully"`
}

type DeleteConditionResponse

type DeleteConditionResponse struct {
	Code    int    `json:"code" example:"200"`
	Message string `json:"message" example:"Condition deleted successfully"`
}

type GetAllConditionsByPageData

type GetAllConditionsByPageData struct {
	Conditions []WebHookConditionRead `json:"conditions" form:"conditions" validate:"required"`
	Total      int64                  `json:"total" form:"total" validate:"required"`
}

type GetAllConditionsByPageRequest

type GetAllConditionsByPageRequest struct {
	Page     int `json:"page" form:"page" binding:"required,gte=0" validate:"required"`
	PageSize int `json:"page_size" form:"page_size" binding:"required,gte=0" validate:"required"`
}

type GetAllConditionsByPageResponse

type GetAllConditionsByPageResponse struct {
	Code    int                        `json:"code" example:"200"`
	Data    GetAllConditionsByPageData `json:"data" `
	Message string                     `json:"message" example:"Condition get successfully"`
}

type GetConditionByIdResponse

type GetConditionByIdResponse struct {
	Code    int                  `json:"code" example:"200"`
	Data    WebHookConditionRead `json:"data" `
	Message string               `json:"message" example:"Condition get successfully"`
}

type GetWebhookConfigByIdResponse

type GetWebhookConfigByIdResponse struct {
	Code    int               `json:"code" example:"200"`
	Data    WebHookConfigRead `json:"data" `
	Message string            `json:"message" example:"Webhook configuration get successfully"`
}

type GetWebhookConfigByNameRequest

type GetWebhookConfigByNameRequest struct {
	Name string `json:"name" form:"name" example:"webhook1"` // webhook名称
}

type GetWebhookConfigByNameResponse

type GetWebhookConfigByNameResponse struct {
	Code    int               `json:"code" example:"200"`
	Data    WebHookConfigRead `json:"data" `
	Message string            `json:"message" example:"Webhook configuration get successfully"`
}

type UpdateConditionRequest

type UpdateConditionRequest struct {
	And  []globaldata.Condition `json:"and" form:"and"`   // 规则列表,逻辑与
	Or   []globaldata.Condition `json:"or" form:"or"`     // 规则列表,逻辑或
	Rule *globaldata.Rule       `json:"rule" form:"rule"` // 规则
}

type UpdateConditionResponse

type UpdateConditionResponse struct {
	Code    int                  `json:"code" example:"200"`
	Data    WebHookConditionRead `json:"data" `
	Message string               `json:"message" example:"Condition updated successfully"`
}

type WebHookConditionRead

type WebHookConditionRead struct {
	Id        int64     `json:"id" form:"id" validate:"required"`
	Condition string    `json:"condition" form:"condition" validate:"required"`
	CreatedAt time.Time `json:"created_at" form:"created_at" validate:"required"`
	UpdatedAt time.Time `json:"updated_at" form:"updated_at" validate:"required"`
}

func ServiceCreateCondition

func ServiceCreateCondition(req *CreateConditionRequest) WebHookConditionRead

func ServiceGetConditionById

func ServiceGetConditionById(id string) WebHookConditionRead

func ServiceUpdateCondition

func ServiceUpdateCondition(id string, req *UpdateConditionRequest) WebHookConditionRead

type WebHookConfigRead

type WebHookConfigRead struct {
	Id           int64     `json:"id" form:"id" validate:"required"`
	Name         string    `json:"name" form:"name" validate:"required"`
	Url          string    `json:"url" form:"url" validate:"required"`
	Active       bool      `json:"active" form:"active" validate:"required"`
	When         *string   `json:"when" form:"when" validate:"omitempty"`
	NeedNodeList []string  `validate:"required"`
	ConditionId  *int64    `json:"condition_id" form:"condition_id" validate:"omitempty"`
	CreatedAt    time.Time `json:"created_at" form:"created_at" validate:"required"`
	UpdatedAt    time.Time `json:"updated_at" form:"updated_at" validate:"required"`
}

func GetAllWebhookConfigFromDB

func GetAllWebhookConfigFromDB() []WebHookConfigRead

func GetWebhookConfigByIdFromDB

func GetWebhookConfigByIdFromDB(id int64) WebHookConfigRead

func ServiceAddWebhookConfig

func ServiceAddWebhookConfig(req *AddWebhookConfigRequest) WebHookConfigRead

func ServiceGetWebhookConfigById

func ServiceGetWebhookConfigById(id string) WebHookConfigRead

type WebHookExampleRequest

type WebHookExampleRequest struct {
	Values    map[string]interface{} `json:"values" form:"values" binding:"required"`       // 节点值 any类型
	TimeStamp int64                  `json:"timestamp" form:"timestamp" binding:"required"` // 时间戳 int64类型
}

type WebHookExampleResponse

type WebHookExampleResponse struct {
	Code    int    `json:"code" example:"200"`
	Data    string `json:"data" example:"webhook example"`
	Message string `json:"message" example:"webhook example success"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL