-
Notifications
You must be signed in to change notification settings - Fork 15
/
handlers.go
160 lines (132 loc) · 3.79 KB
/
handlers.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"encoding/xml"
"errors"
"os"
"net/http"
"github.com/thenrich/rooms/conf"
"github.com/thenrich/rooms/logger"
"strconv"
)
// REST API handler for incoming conference room requests
type IncomingConferenceHandler struct{}
func (b *IncomingConferenceHandler) Handle(appCtx *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {
switch r.Method {
case "GET":
return b.get(appCtx, w, r)
case "POST":
return b.post(appCtx, w, r)
default:
return http.StatusMethodNotAllowed, errors.New("Method not allowed")
}
}
func (b *IncomingConferenceHandler) ContentType() string {
return "text/xml"
}
// Return Twilio XML for providing a conference ID
func (b *IncomingConferenceHandler) get(appCtx *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !conf.VerifyTwilioRequest(r, appCtx.TwilioConfig.Key) {
return http.StatusBadRequest, errors.New("Invalid Twilio auth")
}
type Response struct {
Gather struct {
NumDigits int64 `xml:"numDigits,attr"`
Action string `xml:"action,attr"`
Method string `xml:"method,attr"`
Timeout int64 `xml:"timeout,attr"`
Say string
}
Redirect string
}
out := &Response{
Gather: struct {
NumDigits int64 `xml:"numDigits,attr"`
Action string `xml:"action,attr"`
Method string `xml:"method,attr"`
Timeout int64 `xml:"timeout,attr"`
Say string
}{
NumDigits: appCtx.TwilioConfig.ConferenceRoomNumDigits,
Action: fmt.Sprintf("%s/calls/conferences", appCtx.TwilioConfig.BaseUrl),
Method: "POST",
Timeout: appCtx.TwilioConfig.ConferenceRoomTimeout,
Say: "Please enter your 6-digit conference ID.",
},
Redirect: "",
}
w.Write([]byte(xml.Header))
xml.NewEncoder(w).Encode(out)
return http.StatusOK, nil
}
func (b *IncomingConferenceHandler) post(appCtx *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {
if !conf.VerifyTwilioRequest(r, appCtx.TwilioConfig.Key) {
return http.StatusBadRequest, errors.New("Invalid Twilio auth")
}
w.Write([]byte(xml.Header))
xml.NewEncoder(w).Encode(startConference(r.FormValue("Digits")))
return http.StatusOK, nil
}
func startConference(roomNumber string) interface{} {
type Response struct {
Dial struct {
Conference string
}
}
out := &Response{
Dial: struct {
Conference string
}{
Conference: fmt.Sprintf("Room %s", roomNumber),
},
}
return out
}
type restHandler interface {
Handle(appCtx *AppContext, w http.ResponseWriter, r *http.Request) (int, error)
ContentType() string
}
type appHandler struct {
*AppContext
Handler restHandler
}
func NewAppHandler(hndlr restHandler) *appHandler {
return &appHandler{
&AppContext{},
hndlr,
}
}
func (rh *appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Add context to AppContext
rh.AppContext.Ctx = AppEngineContext(r)
logger.DefaultLogger.SetContext(rh.AppContext.Ctx)
roomDigits, err := strconv.Atoi(os.Getenv("ROOM_NUM_DIGITS"))
if err != nil {
logger.DefaultLogger.Errorf("Can not convert room digits")
}
roomTimeout, err := strconv.Atoi(os.Getenv("ROOM_NUM_TIMEOUT"))
if err != nil {
logger.DefaultLogger.Errorf("Can not convert room timeout")
}
// Add Twilio config
rh.AppContext.TwilioConfig = conf.NewTwilioConfig(
[]byte(os.Getenv("TWILIO_API_KEY")),
os.Getenv("BASE_URL"),
int64(roomDigits),
int64(roomTimeout),
)
// Always set application/json
w.Header().Set("Content-Type", rh.Handler.ContentType())
status, err := rh.Handler.Handle(rh.AppContext, w, r)
if err != nil {
logger.DefaultLogger.Infof("HTTP %d: %q", status, err)
switch status {
case http.StatusNotFound:
http.NotFound(w, r)
case http.StatusInternalServerError:
http.Error(w, http.StatusText(status), status)
default:
http.Error(w, http.StatusText(status), status)
}
}
}