-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
245 lines (222 loc) · 5.96 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
)
var (
processLock = false // http requests processed via goroutines. We don't handle parallel applescript though
ErrBusy = errors.New("the server is currently busy processing another request")
ErrMalformattedLink = errors.New("the url could not be validated against the facetime url format")
)
type status struct {
ServerStatus string `json:"server_status"`
FacetimeStatus bool `json:"facetime_status"`
Busy bool `json:"busy"`
}
type linkInfo struct {
Link ftLink `json:"link"`
}
type linkJoinInfo struct {
Link ftLink `json:"link"`
Joined bool `json:"joined"`
}
type linkAdmitInfo struct {
Admitted bool `json:"admitted"`
}
type linkLeaveInfo struct {
Left bool `json:"left"`
}
type linkDeleteInfo struct {
Link ftLink `json:"link"`
Deleted bool `json:"deleted"`
}
func main() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.ForwardedByClientIP = true
prox := strings.Split(os.Getenv("TRUSTED_PROXIES"), ",")
r.SetTrustedProxies(prox)
r.GET("/status", routeGetStatus)
r.GET("/link", routeGetActiveLinks)
r.POST("/link/new", routeNewLink)
r.POST("/link/join", routeJoinLink)
r.POST("/link/admit", routeAdmitLink)
r.POST("/link/leave", routeLeaveLink)
r.DELETE("/link", routeDeleteLink)
openFacetime()
r.Run("localhost:8080")
}
func routeGetStatus(c *gin.Context) {
res := http.StatusOK
fSt, err := getFacetimeStatus()
if err != nil {
fmt.Println(err)
res = http.StatusInternalServerError
}
c.IndentedJSON(res, status{ServerStatus: "running", FacetimeStatus: fSt, Busy: processLock})
}
func routeGetActiveLinks(c *gin.Context) {
err := initSession()
defer closeSession()
if err == ErrBusy {
fmt.Println(err)
c.AbortWithStatus(http.StatusServiceUnavailable)
return
} else if err != nil {
fmt.Println("Unhandled error in initializing FaceTime:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
allLinks, err := getAllLinks()
if err != nil {
fmt.Println("Failed to retrieve current links:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
allFtLinksInfo := []linkInfo{}
badLinks := false
for _, l := range allLinks {
ftL := ftLink(l)
ftUrl, err := ftL.getUrl()
if err != nil {
badLinks = true
fmt.Println("Got invalid link: '" + l + "'")
continue
}
newFtLinkInfo := linkInfo{Link: ftUrl}
allFtLinksInfo = append(allFtLinksInfo, newFtLinkInfo)
}
if badLinks {
fmt.Println("Found links but unable to validate formatting of any.")
c.AbortWithStatus(http.StatusInternalServerError)
}
c.JSON(http.StatusOK, allFtLinksInfo)
}
func routeNewLink(c *gin.Context) {
err := initSession()
defer closeSession()
if err == ErrBusy {
fmt.Println(err)
c.AbortWithStatus(http.StatusServiceUnavailable)
return
} else if err != nil {
fmt.Println("Unhandled error in initializing FaceTime:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
newLink, err := makeNewLink()
if err != nil {
fmt.Println("Failed to make new link:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
newFtLink := ftLink(newLink)
_, err = newFtLink.getUrl()
if err != nil {
fmt.Print("Got an invalid link:'" + newLink + "'\n")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, linkInfo{Link: newFtLink})
}
func routeJoinLink(c *gin.Context) {
err := initSession()
defer closeSession()
if err == ErrBusy {
fmt.Println(err)
c.AbortWithStatus(http.StatusServiceUnavailable)
return
} else if err != nil {
fmt.Println("Unhandled error in initializing FaceTime:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = joinAndAdmitCall()
if err != nil {
fmt.Println("Failed to join link:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, linkJoinInfo{Link: "", Joined: true})
}
func routeAdmitLink(c *gin.Context) {
err := initSession()
defer closeSession()
if err == ErrBusy {
fmt.Println(err)
c.AbortWithStatus(http.StatusServiceUnavailable)
return
} else if err != nil {
fmt.Println("Unhandled error in initializing FaceTime:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = admitActiveCall()
if err != nil {
fmt.Println("Failed to admit participants to link:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, linkAdmitInfo{Admitted: true})
}
func routeLeaveLink(c *gin.Context) {
err := initSession()
defer closeSession()
if err == ErrBusy {
fmt.Println(err)
c.AbortWithStatus(http.StatusServiceUnavailable)
return
} else if err != nil {
fmt.Println("Unhandled error in initializing FaceTime:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = leaveCall()
if err != nil {
fmt.Println("Failed to have host leave call:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, linkLeaveInfo{Left: true})
}
func routeDeleteLink(c *gin.Context) {
var requestInfo linkInfo
err := c.BindJSON(&requestInfo)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
linkD, err := requestInfo.Link.getUrl()
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
idD := linkD.getId()
err = initSession()
defer closeSession()
if err == ErrBusy {
fmt.Println(err)
c.AbortWithStatus(http.StatusServiceUnavailable)
return
} else if err != nil {
fmt.Println("Unhandled error in initializing FaceTime:", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
wasDeleted, err := deleteCall(string(idD))
var status int
if err != nil {
fmt.Println("Link", linkD, "was not able to be deleted due to an error:", err)
status = http.StatusInternalServerError
} else if !wasDeleted {
fmt.Println("Link", linkD, "was not found for deletion.")
status = http.StatusNotFound
} else {
status = http.StatusOK
}
c.JSON(status, linkDeleteInfo{Link: linkD, Deleted: wasDeleted})
}