Skip to content

Instantly share code, notes, and snippets.

@wudi
Created September 18, 2016 08:18
Show Gist options
  • Select an option

  • Save wudi/a8f68952ecc69337cdc2a772d6370ec6 to your computer and use it in GitHub Desktop.

Select an option

Save wudi/a8f68952ecc69337cdc2a772d6370ec6 to your computer and use it in GitHub Desktop.
Simple Webhook (exec shell script)
package main
import (
"net/http"
"log"
"flag"
"os"
"os/exec"
"sync"
"fmt"
)
var SHELL = ""
var PORT = 10086
var mu sync.Mutex
func init() {
flag.StringVar(&SHELL, "shell", SHELL, "exec shell script when call deploy")
flag.IntVar(&PORT, "port", PORT, "Listen port")
flag.Parse()
}
func main() {
if len(SHELL) == 0 {
log.Fatal("Unknown shell script")
}
if _, err := os.Stat(SHELL); os.IsNotExist(err) {
log.Fatalf("Shell script not found, %s", SHELL)
}
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
out, err := exec.Command(SHELL).Output()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
w.Write(out)
}
mu.Unlock()
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment