Compare commits

...

6 commits

Author SHA1 Message Date
59e1445754
Change clone address from ssh to http
To use ssh you need to have an account...
2022-03-13 15:28:16 +01:00
1d2c2bd09d
Add note about the usage 2022-03-13 15:24:06 +01:00
ce450fed51
Fix error message formatting 2022-03-13 15:21:21 +01:00
bdf8c1f0f3
Remove listener for os.Kill
It's pointless to listen for that signal because the kernel will not
wait til the process finishes.
2022-03-13 15:19:49 +01:00
fe9e4c2f34
Add command output to the response 2022-03-13 15:17:57 +01:00
753b9ec7a7
Add context to command to be canceled if the connection is closed 2022-03-13 15:16:18 +01:00
2 changed files with 9 additions and 7 deletions

View file

@ -9,7 +9,7 @@ A simple and naive implementation of a web server for the usage with webhooks.
- Go >= 1.17 - Go >= 1.17
```sh ```sh
git clone ssh://git@git.hhhammer.de:2222/hamburghammer/whook.git git clone https://git.hhhammer.de/hamburghammer/whook.git
cd whook cd whook
go build go build
``` ```
@ -19,7 +19,7 @@ You should now have a `whook` executable in the directory.
## Usage ## Usage
The server is build to run behind a proxy that provides `https` and some kind of The server is build to run behind a proxy that provides `https` and some kind of
`auth`. `auth` and it is not designed for long running processes.
```sh ```sh
./whook -c ./test.sh ./whook -c ./test.sh

12
main.go
View file

@ -39,7 +39,7 @@ func init() {
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() // so I don't forget to close it -> eventhough it's not read. defer r.Body.Close() // so I don't forget to close it -> eventhough it's not read.
cmd := exec.Command(cmd, cmdArgs...) cmd := exec.CommandContext(r.Context(), cmd, cmdArgs...)
// set directory only if it's present. // set directory only if it's present.
if commandExecDir != "" { if commandExecDir != "" {
cmd.Dir = commandExecDir cmd.Dir = commandExecDir
@ -47,10 +47,12 @@ func handler(w http.ResponseWriter, r *http.Request) {
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
fmt.Fprint(w, err.Error())
w.WriteHeader(http.StatusBadGateway) w.WriteHeader(http.StatusBadGateway)
} }
log.Println("Command output:\n" + string(out)) output := string(out)
log.Println("Command output:\n" + output)
fmt.Fprint(w, output)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
@ -103,7 +105,7 @@ func startHTTPServer(server *http.Server, wg *sync.WaitGroup) {
func listenToStopHTTPServer(server *http.Server, wg *sync.WaitGroup) { func listenToStopHTTPServer(server *http.Server, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, os.Kill) signal.Notify(stop, os.Interrupt)
<-stop // block til signal is captured. <-stop // block til signal is captured.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@ -127,7 +129,7 @@ FLAGS:`)
// validateParams if required params are present. // validateParams if required params are present.
func validateParams() error { func validateParams() error {
if cmd == "" { if cmd == "" {
return errors.New("Missing required 'cmd' parameter") return errors.New("missing required 'cmd' parameter")
} }
return nil return nil