-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (80 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
type hook struct {
Name string
URL string
Command string
Full string
}
type configObject struct {
Secretkey string
Port string
Hooks []hook
}
func (c configObject) command(url string) {
for _, hook := range c.Hooks {
if hook.Full == url {
cmdlet := strings.Fields(hook.Command)
if len(cmdlet) == 1 {
cmd := exec.Command(cmdlet[0])
cmd.Run()
log.Println("Ran: " + hook.Command)
} else {
cmd := exec.Command(cmdlet[0], cmdlet[1:]...)
cmd.Run()
log.Println("Ran: " + hook.Command)
}
}
}
}
func (h hook) print() {
log.Println("Configured new hook!")
log.Println("\tName:" + h.Name)
log.Println("\tCommand:" + h.Command)
log.Println("\tURL:" + h.Full)
}
func main() {
file, e := ioutil.ReadFile("./config.json")
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
f, err := os.OpenFile("./webhooks.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
log.Println("\n\n\n\t")
var config configObject
json.Unmarshal(file, &config)
hooks := config.Hooks
for k := range hooks {
hp := &hooks[k]
hp.Full = "/" + config.Secretkey + "/" + hooks[k].URL
http.HandleFunc(hooks[k].Full, func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
} else {
log.Println("Recieved POST response from: " + r.RemoteAddr)
log.Println("Recieved POST response: " + string(body))
}
}
config.command(r.RequestURI)
})
hooks[k].print()
}
port := ":" + string(config.Port)
http.ListenAndServe(port, nil)
}