-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (60 loc) · 1.45 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/akerl/go-lambda/apigw/events"
"github.com/akerl/go-lambda/mux"
"github.com/akerl/go-lambda/s3"
)
type target struct {
URL string `json:"url"`
Method string `json:"method,omitempty"`
}
type hook struct {
Targets []target `json:"targets"`
}
type config struct {
Hooks map[string]hook `json:"hooks"`
}
var c config
func handler(req events.Request) (events.Response, error) {
hookID := req.PathParameters["hook"]
if hookID == "" {
return events.Fail("hook id not set")
}
h, ok := c.Hooks[hookID]
if !ok {
return events.Fail(fmt.Sprintf("hook not found in config: %s", hookID))
}
log.Printf("processing hook: %s", hookID)
client := http.Client{}
for _, t := range h.Targets {
targetReq, err := http.NewRequest(t.Method, t.URL, nil)
if err != nil {
return events.Fail(fmt.Sprintf("failed to parse request: %s", t.URL))
}
_, err = client.Do(targetReq)
if err != nil {
return events.Fail(fmt.Sprintf("failed to hit url: %s (%s)", t.URL, err))
}
}
log.Printf("successfully hit %d urls", len(h.Targets))
return events.Succeed(fmt.Sprintf("Processed %d URLs", len(h.Targets)))
}
func loadConfig() {
cf, err := s3.GetConfigFromEnv(&c)
if err != nil {
log.Print(err)
return
}
cf.OnError = func(_ *s3.ConfigFile, err error) {
log.Print(err)
}
cf.Autoreload(60)
}
func main() {
loadConfig()
d := mux.NewReceiver(mux.NoCheck, mux.NoAuth, handler, mux.NoError)
mux.Start(d)
}