Skip to content

Commit

Permalink
add WEBHOOK_OUTPUT_STDOUT WEBHOOK_OUTPUT_STDERR Env
Browse files Browse the repository at this point in the history
  • Loading branch information
t-matsuo committed Aug 25, 2022
1 parent bcf8426 commit 61fb9a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ It receives webhook and call specified command.

* WEBHOOK_CMD (required)
* command
* WEBHOOK_OUTPUT_STDOUT (default:false)
* Output stdout of command to client
* WEBHOOK_OUTPUT_STDERR (default:false)
* Output stderr of command to client if command does not exit with 0
* WEBHOOK_PORT (default:22999)
* Listen Port
* WEBHOOK_BIND (default:0.0.0.0)
Expand Down
45 changes: 32 additions & 13 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ import (
)

var goenv struct {
Cmd string `required:"true"`
Port int `default:"22999"`
Bind string `default:"0.0.0.0"`
Path string `default:"/"`
Debug bool `default:"false"`
Log_prefix string `default:"[webhook]"`
No_alog bool `default:"false"`
Timeout int `default:"300"`
Workdir string `default:"/tmp"`
Tls bool `default:"false"`
Server_crt string `default:""`
Server_key string `default:""`
Cmd string `required:"true"`
Output_stdout bool `default:"false"`
Output_stderr bool `default:"false"`
Port int `default:"22999"`
Bind string `default:"0.0.0.0"`
Path string `default:"/"`
Debug bool `default:"false"`
Log_prefix string `default:"[webhook]"`
No_alog bool `default:"false"`
Timeout int `default:"300"`
Workdir string `default:"/tmp"`
Tls bool `default:"false"`
Server_crt string `default:""`
Server_key string `default:""`
}

var log_info *log.Logger
Expand Down Expand Up @@ -101,6 +103,8 @@ func handleEnv() {
}

log_info.Printf("Command is %s\n", goenv.Cmd)
log_info.Printf("Output stdout is %t\n", goenv.Output_stdout)
log_info.Printf("Output stderr is %t\n", goenv.Output_stderr)
log_info.Printf("Workdir is %s\n", goenv.Workdir)
if goenv.Tls == false {
log_info.Printf("Listening on http://%s:%d%s\n", goenv.Bind, goenv.Port, goenv.Path)
Expand Down Expand Up @@ -154,15 +158,30 @@ func handleReq(w http.ResponseWriter, r *http.Request) {
"WEBHOOK_PATH="+path,
)
cmd.Dir = goenv.Workdir

var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()

if err != nil {
log_err.Printf("Internal Server Error. %s\n", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Internal Server Error\n")
if goenv.Output_stderr {
fmt.Fprintf(w, "\n############### stderr ###############\n%s\n", stderr.String())
}
if goenv.Output_stdout {
fmt.Fprintf(w, "\n############### stdout ###############\n%s\n", stdout.String())
}
} else {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK\n")
if goenv.Output_stdout {
fmt.Fprintf(w, "%s", stdout.String())
} else {
fmt.Fprintf(w, "OK\n")
}
}
default:
log_access.Printf("%s %s Method Not Allowed\n", alog_format, r.Method)
Expand Down

0 comments on commit 61fb9a4

Please sign in to comment.