Skip to content

Commit 1be314e

Browse files
committed
add syslog support in webhook mock
1 parent add9eba commit 1be314e

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

docs/site/content/zh/latest/tasks/mock.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,11 @@ proxies:
196196

197197
当前代理支持 HTTP 和 TCP 协议,上面的例子中代理了 MySQL 的 `33060` 端口。
198198

199+
## Webhook
200+
201+
有些场景下,需要定时向服务器发送请求,这时可以使用 Webhook。当前支持的协议包括:
202+
203+
* HTTP
204+
* Syslog
205+
199206
> 更多 URL 中通配符的用法,请参考 https://github.com/gorilla/mux
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
webhooks:
2+
- timer: 3s
3+
name: syslog
4+
request:
5+
protocol: syslog
6+
path: 192.168.123.58:5140
7+
body: |
8+
{
9+
"level": "{{ randEnum "FATAL" "ERROR" "WARNING" "INFO" }}",
10+
"message": "{{ randAlphaNum 10 }}"
11+
}

pkg/mock/in_memory.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,6 @@ func (s *inMemoryServer) startWebhook(webhook *Webhook) (err error) {
508508
}
509509

510510
func runWebhook(ctx context.Context, objCtx interface{}, wh *Webhook) (err error) {
511-
client := http.DefaultClient
512-
513511
rawParams := make(map[string]string, len(wh.Param))
514512
paramKeys := make([]string, 0, len(wh.Param))
515513
for k, v := range wh.Param {
@@ -533,14 +531,34 @@ func runWebhook(ctx context.Context, objCtx interface{}, wh *Webhook) (err error
533531
}
534532
wh.Param = rawParams
535533

536-
method := util.EmptyThenDefault(wh.Request.Method, http.MethodPost)
537534
var api string
538535
api, err = render.Render("webhook request api", wh.Request.Path, objCtx)
539536
if err != nil {
540537
err = fmt.Errorf("error when render api: %w, template: %s", err, wh.Request.Path)
541538
return
542539
}
543540

541+
switch wh.Request.Protocol {
542+
case "syslog":
543+
err = sendSyslogWebhookRequest(ctx, wh, api, payload)
544+
default:
545+
err = sendHTTPWebhookRequest(ctx, wh, api, payload)
546+
}
547+
return
548+
}
549+
550+
func sendSyslogWebhookRequest(ctx context.Context, wh *Webhook, api string, payload io.Reader) (err error) {
551+
var conn net.Conn
552+
if conn, err = net.Dial("udp", api); err == nil {
553+
_, err = io.Copy(conn, payload)
554+
}
555+
return
556+
}
557+
558+
func sendHTTPWebhookRequest(ctx context.Context, wh *Webhook, api string, payload io.Reader) (err error) {
559+
method := util.EmptyThenDefault(wh.Request.Method, http.MethodPost)
560+
client := http.DefaultClient
561+
544562
var bearerToken string
545563
bearerToken, err = getBearerToken(ctx, wh.Request)
546564
if err != nil {

pkg/mock/types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ type Item struct {
2929
}
3030

3131
type Request struct {
32-
Path string `yaml:"path" json:"path"`
33-
Method string `yaml:"method" json:"method"`
34-
Header map[string]string `yaml:"header" json:"header"`
35-
Body string `yaml:"body" json:"body"`
32+
Protocol string `yaml:"protocol" json:"protocol"`
33+
Path string `yaml:"path" json:"path"`
34+
Method string `yaml:"method" json:"method"`
35+
Header map[string]string `yaml:"header" json:"header"`
36+
Body string `yaml:"body" json:"body"`
3637
}
3738

3839
type RequestWithAuth struct {

0 commit comments

Comments
 (0)