-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandlers.go
77 lines (65 loc) · 2.16 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
package main
import (
"net/http"
"os"
"reflect"
"strings"
"github.com/gin-gonic/gin"
)
func getREADME(c *gin.Context) {
readme, err := os.ReadFile("page.html")
if err != nil {
c.Data(http.StatusInternalServerError, "text/html; charset=utf-8", []byte("an internal error occurred"))
}
c.Data(http.StatusOK, "text/html; charset=utf-8", readme)
}
func getAllData(c *gin.Context) {
packageID := c.Query("id")
if packageID == "" {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "package id is mandatory"})
return
}
resBody, statusCode := fetchHTML(packageID)
if statusCode == http.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "package id is invalid"})
return
} else if statusCode != 200 {
c.IndentedJSON(http.StatusBadGateway, gin.H{"message": "an internal error occurred"})
return
}
parsedPlaystoreData, err := parsePlaystoreData(packageID, resBody)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "an internal error occurred"})
return
}
c.IndentedJSON(http.StatusOK, *parsedPlaystoreData)
}
func getDataByKey(c *gin.Context) {
packageID := c.Query("id")
if packageID == "" {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "package id is mandatory"})
return
}
resBody, statusCode := fetchHTML(packageID)
if statusCode == http.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "package id is invalid"})
return
} else if statusCode != 200 {
c.IndentedJSON(http.StatusBadGateway, gin.H{"message": "an internal error occurred"})
return
}
parsedPlaystoreData, err := parsePlaystoreData(packageID, resBody)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "an internal error occurred"})
return
}
key := c.Params.ByName("key")
val := reflect.ValueOf(*parsedPlaystoreData)
for i := 0; i < val.Type().NumField(); i++ {
if strings.EqualFold(key, val.Type().Field(i).Tag.Get("json")) {
c.IndentedJSON(http.StatusOK, gin.H{"schemaVersion": 1, "label": val.Type().Field(i).Tag.Get("api"), "message": val.Field(i).Interface()})
return
}
}
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "key is invalid"})
}