Compare commits
6 commits
3e162a3b82
...
59e1445754
Author | SHA1 | Date | |
---|---|---|---|
59e1445754 | |||
1d2c2bd09d | |||
ce450fed51 | |||
bdf8c1f0f3 | |||
fe9e4c2f34 | |||
753b9ec7a7 |
2 changed files with 9 additions and 7 deletions
|
@ -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
12
main.go
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue