-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchickenlittle.go
128 lines (94 loc) · 2.95 KB
/
chickenlittle.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
)
var (
cfgFile *string
c ChickenLittle
NIP NotificationsInProgress
planChan = make(chan *NotificationRequest)
)
type ChickenLittle struct {
Config Config
DB DB
}
func main() {
cfgFile = flag.String("config", "config.yaml", "Path to config file (default: ./config.yaml)")
flag.Parse()
// Read our server configuration
filename, _ := filepath.Abs(*cfgFile)
cfgFile, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalln("Error opening config file. Did you pass the -config flag? Run with -h for help.\n", err)
}
err = yaml.Unmarshal(cfgFile, &c.Config)
if err != nil {
log.Fatalln("Error:", err)
}
// Open our BoltDB handle
c.DB.Open(c.Config.Service.DBFile)
defer c.DB.Close()
// Create our stop channel and launch the notification engine
stopChan = make(chan string)
go StartNotificationEngine()
// Set up our API endpoint router
go func() {
log.Fatal(http.ListenAndServe(c.Config.Service.APIListenAddr, apiRouter()))
}()
// Set up our Twilio callback endpoint router
go func() {
log.Fatal(http.ListenAndServe(c.Config.Service.CallbackListenAddr, callbackRouter()))
}()
// Set up our Click endpoint router to handle stop requests from browsers
log.Fatal(http.ListenAndServe(c.Config.Service.ClickListenAddr, clickRouter()))
}
func apiRouter() *mux.Router {
apiRouter := mux.NewRouter().StrictSlash(true)
apiRouter.HandleFunc("/people", ListPeople).
Methods("GET")
apiRouter.HandleFunc("/people", CreatePerson).
Methods("POST")
apiRouter.HandleFunc("/people/{person}", ShowPerson).
Methods("GET")
apiRouter.HandleFunc("/people/{person}", DeletePerson).
Methods("DELETE")
apiRouter.HandleFunc("/people/{person}", UpdatePerson).
Methods("PUT")
apiRouter.HandleFunc("/plan/{person}", CreateNotificationPlan).
Methods("POST")
apiRouter.HandleFunc("/plan/{person}", ShowNotificationPlan).
Methods("GET")
apiRouter.HandleFunc("/plan/{person}", DeleteNotificationPlan).
Methods("DELETE")
apiRouter.HandleFunc("/plan/{person}", UpdateNotificationPlan).
Methods("PUT")
apiRouter.HandleFunc("/people/{person}/notify", NotifyPerson).
Methods("POST")
apiRouter.HandleFunc("/notifications/{uuid}", StopNotification).
Methods("DELETE")
return apiRouter
}
func callbackRouter() *mux.Router {
callbackRouter := mux.NewRouter().StrictSlash(true)
callbackRouter.HandleFunc("/{uuid}/twiml/{action}", GenerateTwiML).
Methods("POST")
callbackRouter.HandleFunc("/{uuid}/callback", ReceiveCallback).
Methods("POST")
callbackRouter.HandleFunc("/{uuid}/digits", ReceiveDigits).
Methods("POST")
callbackRouter.HandleFunc("/sms", ReceiveSMSReply).
Methods("POST")
return callbackRouter
}
func clickRouter() *mux.Router {
clickRouter := mux.NewRouter().StrictSlash(true)
clickRouter.HandleFunc("/{uuid}/stop", StopNotificationClick).
Methods("GET")
return clickRouter
}