added flags to specify the hooks json file path, ip and port the webhook should serve on

This commit is contained in:
Adnan Hajdarevic 2015-01-13 00:59:18 +01:00
parent ffabc5541e
commit c70c21fda7
4 changed files with 19 additions and 9 deletions

0
.gitignore vendored Normal file
View file

View file

@ -103,6 +103,10 @@ func (h *Hook) UnmarshalJSON(j []byte) error {
func New(hookFile string) (*Hooks, error) { func New(hookFile string) (*Hooks, error) {
h := &Hooks{fileName: hookFile} h := &Hooks{fileName: hookFile}
if hookFile == "" {
return h, nil
}
// parse hook file for hooks // parse hook file for hooks
file, e := ioutil.ReadFile(hookFile) file, e := ioutil.ReadFile(hookFile)

View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"net/http" "net/http"
"os/exec" "os/exec"
@ -14,23 +15,28 @@ import (
const ( const (
version string = "1.0.0" version string = "1.0.0"
ip string = ""
port int = 9000
) )
var ( var (
webhooks *hooks.Hooks webhooks *hooks.Hooks
appStart time.Time appStart time.Time
ip = flag.String("ip", "", "ip the webhook server should listen on")
port = flag.Int("port", 9000, "port the webhook server should listen on")
hooksFilename = flag.String("hooks", "hooks.json", "path to the json file containing defined hooks the webhook should serve")
) )
func init() {
flag.Parse()
}
func main() { func main() {
appStart = time.Now() appStart = time.Now()
var e error var e error
webhooks, e = hooks.New("hooks.json") webhooks, e = hooks.New(*hooksFilename)
if e != nil { if e != nil {
fmt.Printf("Error while loading hooks from hooks.json:\n\t>>> %s\n", e) fmt.Printf("Error while loading hooks from %s:\n\t>>> %s\n", *hooksFilename, e)
} }
web := martini.Classic() web := martini.Classic()
@ -39,9 +45,9 @@ func main() {
web.Get("/hook/:id", hookHandler) web.Get("/hook/:id", hookHandler)
web.Post("/hook/:id", hookHandler) web.Post("/hook/:id", hookHandler)
fmt.Printf("Starting go-webhook with %d hook(s)\n\n", webhooks.Count()) fmt.Printf("Starting go-webhook with %d hook(s)\n", webhooks.Count())
web.RunOnAddr(fmt.Sprintf("%s:%d", ip, port)) web.RunOnAddr(fmt.Sprintf("%s:%d", *ip, *port))
} }
func rootHandler() string { func rootHandler() string {
@ -49,7 +55,6 @@ func rootHandler() string {
} }
func hookHandler(req *http.Request, params martini.Params) string { func hookHandler(req *http.Request, params martini.Params) string {
decoder := json.NewDecoder(req.Body) decoder := json.NewDecoder(req.Body)
decoder.UseNumber() decoder.UseNumber()
@ -68,5 +73,6 @@ func hookHandler(req *http.Request, params martini.Params) string {
fmt.Printf("Command output for %v >>> %s\n", hook, out) fmt.Printf("Command output for %v >>> %s\n", hook, out)
} }
}(params["id"], p) }(params["id"], p)
return "Got it, thanks. :-)" return "Got it, thanks. :-)"
} }