mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-07 18:16:12 +00:00
chore(): add hook_id example and doc
This commit is contained in:
parent
d3777a7fcd
commit
25fe46c731
|
@ -150,6 +150,7 @@ The script:
|
||||||
```bash
|
```bash
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "Hook information: name=$hook_name, id=$hook_id"
|
||||||
echo "Query parameter: foo=$foo"
|
echo "Query parameter: foo=$foo"
|
||||||
echo "Header parameter: user-agent=$user_agent"
|
echo "Header parameter: user-agent=$user_agent"
|
||||||
echo "Script parameters: $1"
|
echo "Script parameters: $1"
|
||||||
|
@ -159,6 +160,7 @@ The result:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ curl --data @test.json http://localhost:8080/echo?foo=bar
|
$ curl --data @test.json http://localhost:8080/echo?foo=bar
|
||||||
|
Hook information: name=echo, id=1
|
||||||
Query parameter: foo=bar
|
Query parameter: foo=bar
|
||||||
Header parameter: user-agent=curl/7.52.1
|
Header parameter: user-agent=curl/7.52.1
|
||||||
Script parameter: {"foo": "bar"}
|
Script parameter: {"foo": "bar"}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
@ -62,6 +63,14 @@ func NewWorkRequest(name, script, payload, output string, args []string, timeout
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Meta return work request meta
|
||||||
|
func (wr *WorkRequest) Meta() []string {
|
||||||
|
return []string{
|
||||||
|
"hook_id=" + strconv.FormatUint(wr.ID, 10),
|
||||||
|
"hook_name=" + wr.Name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Terminate set work request as terminated
|
// Terminate set work request as terminated
|
||||||
func (wr *WorkRequest) Terminate(err error) error {
|
func (wr *WorkRequest) Terminate(err error) error {
|
||||||
wr.mutex.Lock()
|
wr.mutex.Lock()
|
||||||
|
|
|
@ -2,7 +2,6 @@ package worker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -36,11 +35,12 @@ func Run(work *model.WorkRequest) error {
|
||||||
return work.Terminate(err)
|
return work.Terminate(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec script with args...
|
// Exec script with parameter...
|
||||||
cmd := exec.Command(binary, work.Payload)
|
cmd := exec.Command(binary, work.Payload)
|
||||||
// with env variables...
|
// with env variables and hook arguments...
|
||||||
workEnv := append(os.Environ(), fmt.Sprintf("hook_id=%d", work.ID))
|
cmd.Env = append(os.Environ(), work.Args...)
|
||||||
cmd.Env = append(workEnv, work.Args...)
|
// and hook meta...
|
||||||
|
cmd.Env = append(cmd.Env, work.Meta()...)
|
||||||
// using a process group...
|
// using a process group...
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
# Usage: http POST :8080/echo msg==hello foo=bar
|
# Usage: http POST :8080/echo msg==hello foo=bar
|
||||||
|
|
||||||
echo "Echo script:"
|
echo "This is a simple echo hook."
|
||||||
|
|
||||||
|
echo "Hook information: name=$hook_name, id=$hook_id"
|
||||||
|
|
||||||
echo "Command result: hostname=`hostname`"
|
echo "Command result: hostname=`hostname`"
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,16 @@ const os = require('os')
|
||||||
|
|
||||||
// Usage: http POST :8080/examples/echo.js msg==hello foo=bar
|
// Usage: http POST :8080/examples/echo.js msg==hello foo=bar
|
||||||
|
|
||||||
console.log("Echo script:")
|
console.log("This is a simple echo hook using NodeJS.")
|
||||||
|
|
||||||
|
const { hook_name, hook_id , user_agent, msg} = process.env
|
||||||
|
|
||||||
|
console.log(`Hook information: name=${hook_name}, id=${hook_id}`)
|
||||||
|
|
||||||
console.log("Hostname=", os.hostname())
|
console.log("Hostname=", os.hostname())
|
||||||
|
|
||||||
console.log("User-Agent=", process.env["user_agent"])
|
console.log(`User-Agent=${user_agent}`)
|
||||||
|
|
||||||
console.log("msg=", process.env["msg"])
|
console.log(`msg=${msg}`)
|
||||||
|
|
||||||
console.log("body=", process.argv)
|
console.log("body=", process.argv)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user