-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
252 lines (214 loc) · 5.21 KB
/
api.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
246
247
248
249
250
251
252
package main
import (
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
pressure "github.com/airdispatch/go-pressure"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
// Squirrel.Mac Update Response:
//
// {
// "url": "http://mycompany.com/myapp/releases/myrelease",
// "name": "My Release Name",
// "notes": "Theses are some release notes innit",
// "pub_date": "2013-09-18T12:29:53+01:00",
// }
type Controller404 struct{}
func (u *Controller404) GetResponse(
p *pressure.Request,
l *pressure.Logger,
) (pressure.View, *pressure.HTTPError) {
return nil, pressure.HTTPErrorNotFound
}
type Version string
func (v Version) After(a Version) bool {
vComp := strings.Split(string(v), ".")
aComp := strings.Split(string(a), ".")
if len(vComp) != 3 || len(aComp) != 3 {
fmt.Println("Not valid versions", v, a)
return false
}
majorV, errv := strconv.Atoi(vComp[0])
majorA, erra := strconv.Atoi(aComp[0])
if errv != nil || erra != nil {
return false
}
if majorV != majorA {
return (majorV > majorA)
}
minorV, errv := strconv.Atoi(vComp[1])
minorA, erra := strconv.Atoi(aComp[1])
if errv != nil || erra != nil {
return false
}
if minorV != minorA {
return (minorV > minorA)
}
buildV, errv := strconv.Atoi(vComp[2])
buildA, erra := strconv.Atoi(aComp[2])
if errv != nil || erra != nil {
return false
}
if buildV != buildA {
return (buildV > buildA)
}
return false
}
type Update struct {
Version string `json:"version"`
Changelog string `json:"changelog"`
Download string `json:"download" bson:"-"`
Supports map[string]string `json:"-"`
}
type UpdateController struct {
Collection *mgo.Collection
}
func (u *UpdateController) GetResponse(
p *pressure.Request,
l *pressure.Logger,
) (pressure.View, *pressure.HTTPError) {
var updates []*Update
err := u.Collection.Find(nil).Sort("-version").All(&updates)
if err != nil {
return nil, &pressure.HTTPError{500, "500: Error loading updates"}
}
if len(updates) == 0 ||
!Version(updates[0].Version).After(Version(p.URL["version"])) ||
updates[0].Supports[p.URL["platform"]] == "" {
return nil, &pressure.HTTPError{
Code: 422,
Text: "No updates at this time.",
}
}
updates[0].Download = updates[0].Supports[p.URL["platform"]]
return &APIView{
Method: p.Method,
View: &JSONView{
Content: updates[0],
},
}, nil
}
type Provider struct {
Name string `json:"name"`
Description string `json:"description"`
Url string `json:"url"`
Fingerprint string `json:"fingerprint"`
EncryptionKey string `bson:"encryption_key" json:"encryption_key"`
Proof string `json:"proof"`
Alias string `json:"alias"`
Users int `json:"users"`
}
func IsDebug(req *http.Request) bool {
q := req.URL.RawQuery
values, err := url.ParseQuery(q)
if err != nil {
return false
}
obj := values.Get("debug")
return obj != ""
}
type TrackerController struct {
Collection *mgo.Collection
}
func (u *TrackerController) GetResponse(
p *pressure.Request,
l *pressure.Logger,
) (pressure.View, *pressure.HTTPError) {
var trackers []*Provider
err := u.Collection.Find(map[string]interface{}{
"debug": IsDebug(p.Request),
}).Sort("-users").All(&trackers)
if err != nil {
return nil, &pressure.HTTPError{500, "500: Error loading trackers"}
}
return &APIView{
Method: p.Method,
View: &JSONView{
Content: trackers,
},
}, nil
}
type ServerController struct {
Collection *mgo.Collection
}
func (u *ServerController) GetResponse(
p *pressure.Request,
l *pressure.Logger,
) (pressure.View, *pressure.HTTPError) {
var servers []*Provider
err := u.Collection.Find(map[string]interface{}{
"debug": IsDebug(p.Request),
}).Sort("-users").All(&servers)
if err != nil {
return nil, &pressure.HTTPError{500, "500: Error loading servers"}
}
return &APIView{
Method: p.Method,
View: &JSONView{
Content: servers,
},
}, nil
}
type ApplicationController struct {
Collection *mgo.Collection
}
func (u *ApplicationController) GetResponse(
p *pressure.Request,
l *pressure.Logger,
) (pressure.View, *pressure.HTTPError) {
// Load Query Params
var find map[string]interface{}
query, err := url.ParseQuery(p.Request.URL.RawQuery)
if err != nil {
fmt.Println("Error parsing query", err)
return nil, internalError
}
// Get Query from URL
if query.Get("q") != "" {
find = map[string]interface{}{
"name": bson.RegEx{
Pattern: fmt.Sprintf(`.*(%s).*`, query),
},
}
}
// Find Apps
apps := make([]*App, 0)
err = u.Collection.Find(find).All(&apps)
if err != nil {
return nil, &pressure.HTTPError{500, "500: Error loading servers"}
}
return &APIView{
Method: p.Method,
View: &JSONView{
Content: apps,
},
}, nil
}
type ResolverController struct{}
func (u *ResolverController) GetResponse(
p *pressure.Request,
l *pressure.Logger,
) (pressure.View, *pressure.HTTPError) {
return &pressure.BasicView{
Status: 200,
Text: getServerLocation(
p.URL["url"],
),
}, nil
}
func getServerLocation(url string) string {
_, recs, err := net.LookupSRV("adtp", "tcp", url)
if err != nil {
fmt.Println("Got error looking up server location", err)
return url
}
for _, s := range recs {
return fmt.Sprintf("%s:%d", s.Target, s.Port)
}
return url
}