forked from TonyNiTN/group-24-ECE461
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
479 lines (433 loc) · 11.8 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package main
/*
Provide GET functionality for the following endpoints:
/package/{id}
/package/{id}/rate
/package/byName/{name}
Provide POST functionality for the following endpoints:
/packages
/package/byRegEx
*/
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"sort"
"strings"
"time"
"github.com/Masterminds/semver"
"github.com/golang-jwt/jwt"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
func print_req_info(r *http.Request) {
fmt.Print(r.Method, r.URL, r.Header, r.URL.Query(), r.URL.RawPath, r.Body)
}
func verifyJWT(endpointHandler func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header["X-Authorization"] != nil {
var tk string = r.Header["X-Authorization"][0]
if strings.Contains(tk, "Bearer ") {
tk = strings.Split(tk, " ")[1]
}
token, err := jwt.Parse(tk, func(token *jwt.Token) (interface{}, error) {
_, ok := token.Method.(*jwt.SigningMethodHMAC)
if !ok {
fmt.Print("Error validating JWT")
return_400_packet(w, r)
}
return []byte(os.Getenv("JWT_SECRET")), nil
})
if err != nil {
fmt.Print("Error validating JWT")
return_400_packet(w, r)
return
}
if token.Valid {
endpointHandler(w, r)
} else {
fmt.Print("Token is not valid")
return_400_packet(w, r)
return
}
} else {
fmt.Print("No X-Authorization in header")
return_400_packet(w, r)
return
}
})
}
/*
// Get the packages from the registry
Missing:
- Pagination
*/
func handle_packages(w http.ResponseWriter, r *http.Request) {
print_req_info(r)
db, err := connect()
//db, err := connect_test_db()
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer db.Close()
// parse query for offset (pagination). if empty, return the first page of results
query := r.URL.Query()
offset := query.Get("offset")
if offset == "" {
offset = "1"
}
// parse body for versions to find
var response_arr []PackageQuery
var packages_metadata []PackageMetadata
err = json.NewDecoder(r.Body).Decode(&response_arr)
if err != nil {
fmt.Print(err)
return_404_packet(w, r)
return
}
if len(response_arr) == 1 && response_arr[0].Name == "*" {
// return all packages
packages_metadata = get_all_packages_metadata(w, r, db, response_arr)
} else {
// return packages based on query (pagination)
packages_metadata = get_paginated_packages_metadata(w, r, db, offset, response_arr)
}
if packages_metadata == nil {
// error code is already written
return
}
if len(packages_metadata) == 0 {
fmt.Print("packages response is empty")
}
json.NewEncoder(w).Encode(packages_metadata)
return_200_packet(w, r)
}
// Return this package (ID) from google cloud bucket
func handle_package_id(w http.ResponseWriter, r *http.Request) {
print_req_info(r)
db, err := connect()
//db, err := connect_test_db()
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer db.Close()
var meta PackageMetadata
var bucket_object_name string
var ret_package Package
var data PackageData
vars := mux.Vars(r)
id := vars["id"]
if id == "" {
fmt.Print("Empty {id} in path")
return_404_packet(w, r)
return
}
rows, err := db.Query("SELECT id, name, version FROM packages WHERE id = ?;", id)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&meta.ID, &meta.Name, &meta.Version)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
} else {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - Package does not exist."))
return
}
// if above is successful, there should be at least one row
rows, err = db.Query("SELECT rating_pk FROM packages WHERE id = ?;", id)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer rows.Close()
// bucket object name is the same as the rating pk
for rows.Next() {
err = rows.Scan(&bucket_object_name)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
}
b64contents, err := getBucketObject(os.Getenv("BUCKET_NAME"), bucket_object_name)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
data.Content = string(b64contents)
data.URL = ""
data.JSProgram = ""
ret_package.Metadata = &meta
ret_package.Data = &data
json.NewEncoder(w).Encode(ret_package)
return_200_packet(w, r)
}
// Return the package rating with ID (id from path)
func handle_package_rate(w http.ResponseWriter, r *http.Request) {
print_req_info(r)
db, err := connect()
//db, err := connect_test_db()
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer db.Close()
var ratings PackageRating
vars := mux.Vars(r)
id := vars["id"]
if id == "" {
fmt.Print("id cannot be empty")
return_404_packet(w, r)
return
}
rows, err := db.Query("SELECT A.busFactor, A.correctness, A.rampUp, A.responsiveMaintainer, A.licenseScore, A.goodPinningPractice, A.pullRequest, A.netScore FROM ratings AS A INNER JOIN packages AS B ON A.id = B.rating_pk WHERE B.id = ?;", id)
if err != nil {
fmt.Print(err)
return_400_packet(w, r)
return
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&ratings.BusFactor, &ratings.Correctness, &ratings.RampUp, &ratings.ResponsiveMaintainer, &ratings.LicenseScore, &ratings.GoodPinningPractice, &ratings.PullRequest, &ratings.NetScore)
if err != nil {
fmt.Print(err)
return_400_packet(w, r)
return
}
} else {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - Package does not exist."))
return
}
json.NewEncoder(w).Encode(ratings)
return_200_packet(w, r)
}
// return the package history with package name from path (all versions)
// const mysqlFormat = "2006-01-02 15:04:05"
// const timeFormat = "2006-01-02T15:04:05Z"
func handle_package_byname(w http.ResponseWriter, r *http.Request) {
print_req_info(r)
db, err := connect()
//db, err := connect_test_db()
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer db.Close()
var ret []PackageHistoryEntry
var metadataList []PackageMetadata
var times []string
vars := mux.Vars(r)
name := vars["name"]
if name == "" {
fmt.Print("name cannot be empty")
return_404_packet(w, r)
return
}
// get registry entry from name
rows, err := db.Query("SELECT id, name, version, upload_time FROM packages WHERE name = ?;", name)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer rows.Close()
// get all versions of named package
for rows.Next() {
var timevar string
var md PackageMetadata
if err := rows.Scan(&md.ID, &md.Name, &md.Version, &timevar); err != nil {
// package with name not found
fmt.Print(err)
return_500_packet(w, r)
return
}
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
t, err := time.Parse("2006-01-02T15:04:05Z", timevar)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
timevar = t.Format(time.RFC3339)
metadataList = append(metadataList, md)
times = append(times, timevar)
}
if len(metadataList) == 0 || len(times) == 0 {
fmt.Print("No such package.")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - No such package."))
return
}
// iterate through versions of package and get rest of history
for i, md := range metadataList {
var history PackageHistoryEntry
history.User = &User{Name: "nil", IsAdmin: true}
history.Date, err = time.Parse("2006-01-02T15:04:05Z", times[i])
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
history.PackageMetadata = &md
history.Action = ""
ret = append(ret, history)
}
json.NewEncoder(w).Encode(ret)
return_200_packet(w, r)
}
// return a list of package metadata from package names that match the regex
func handle_package_byregex(w http.ResponseWriter, r *http.Request) {
print_req_info(r)
db, err := connect()
//db, err := connect_test_db()
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer db.Close()
//grab RegEx from body
var body PackageRegExBody
var retList []RegExReturn
var listoflists [][]PackageMetadata
err = json.NewDecoder(r.Body).Decode(&body)
if err != nil {
fmt.Print(err)
return_404_packet(w, r)
return
}
// for list of names that match regex, get metadata and append to list of metadata
sqlquery := `SELECT id, name, version FROM packages WHERE name REGEXP "` + body.RegEx + `";`
rows, err := db.Query(sqlquery)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
defer rows.Close()
for rows.Next() {
var md PackageMetadata
var cont bool = false
if err := rows.Scan(&md.ID, &md.Name, &md.Version); err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
for _, md_list := range listoflists {
// might need to check first iteration
// all temps will have same name so only check one
if md_list[0].Name == md.Name {
cont = true
break
}
}
if !cont {
mdl, err := getMetadataFromName(db, md.Name)
if err != nil {
fmt.Print(err)
return_500_packet(w, r)
return
}
if len(mdl) != 0 {
listoflists = append(listoflists, mdl)
}
}
}
// check if no packages are found
if len(listoflists) == 0 {
fmt.Print("No package found under this regex.")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - No package found under this regex."))
return
}
for i, md_list := range listoflists {
var sortedVersions []*semver.Version
for _, md := range md_list {
// create a semantic version for each version
sv, err := semver.NewVersion(md.Version)
if err != nil {
fmt.Print(err)
return_413_packet(w, r)
return
}
sortedVersions = append(sortedVersions, sv)
}
// check if versions are sorted
sort.Sort(semver.Collection(sortedVersions))
// exact version found
if len(sortedVersions) == 1 {
var ret RegExReturn
ret.Version = md_list[0].Version
ret.Name = md_list[0].Name
retList = append(retList, ret)
} else {
// get begin and end of list
begin := sortedVersions[0]
end := sortedVersions[len(sortedVersions)-1]
begin_split := strings.Split(begin.String(), ".")
end_split := strings.Split(end.String(), ".")
var ret RegExReturn
// for bounded range (up to major version can change):
// if first group IS different
if begin_split[0] != end_split[0] {
ret.Version = begin.String() + "-" + end.String()
ret.Name = md_list[i].Name
} else if begin_split[1] != end_split[1] {
// for caret range (only major must match, up to minor version can change):
ret.Version = "^" + begin.String()
ret.Name = md_list[i].Name
} else if begin_split[2] != end_split[2] {
// for tilde range (major and minor must match, patch version can change):
ret.Version = "~" + begin.String()
ret.Name = md_list[i].Name
}
retList = append(retList, ret)
}
}
json.NewEncoder(w).Encode(retList)
return_200_packet(w, r)
}
func handleRequests() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/packages", verifyJWT(handle_packages)).Methods("POST")
router.HandleFunc("/package/{id}", verifyJWT(handle_package_id)).Methods("GET")
router.HandleFunc("/package/{id}/rate", verifyJWT(handle_package_rate)).Methods("GET")
router.HandleFunc("/package/byName/{name}", verifyJWT(handle_package_byname)).Methods("GET")
router.HandleFunc("/package/byRegEx", verifyJWT(handle_package_byregex)).Methods("POST")
c := cors.New(cors.Options{
AllowedOrigins: []string{"https://growyourgrove.tech", "https://www.growyourgrove.tech"},
AllowCredentials: true,
AllowedMethods: []string{"POST", "GET", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*", "X-Authorization"},
// Enable Debugging for testing, consider disabling in production
Debug: true,
})
cors_handler := c.Handler(router)
log.Fatal(http.ListenAndServe(":8080", cors_handler))
}
func main() {
handleRequests()
}