-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (65 loc) · 1.42 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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/ghodss/yaml"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/tylerb/graceful"
"gopkg.in/urfave/cli.v2"
"io/ioutil"
"net/http"
"os"
"time"
)
func actionRun(cc *cli.Context) error {
if cc.Bool("help") {
return cli.ShowAppHelp(cc)
}
args := cc.Args()
if args.Len() == 0 {
return errors.New("missing argument: config.yml")
}
configData, err := ioutil.ReadFile(args.First())
if err != nil {
return err
}
var config Config
if err := yaml.Unmarshal(configData, &config); err != nil {
return err
}
router := httprouter.New()
router.GET("/health", func(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
rw.Write([]byte(`{"ok": true}`))
})
for _, hook := range config.Hooks {
path := fmt.Sprintf("/%s/%s", hook.Type, hook.Key)
router.POST(path, HandleInvokeHook(hook))
log.WithField("path", path).Info("Registered Hook")
}
return graceful.ListenAndServe(
&http.Server{
Addr: "0.0.0.0:8000",
Handler: router,
},
10*time.Second,
)
}
func main() {
app := &cli.App{
Name: "grappler",
Usage: "discord webhook adapter",
ArgsUsage: "config.yml",
Action: actionRun,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "help",
Aliases: []string{"h"},
Usage: "print help and exit",
},
},
HideHelp: true,
HideVersion: true,
}
app.Run(os.Args)
}