-
Notifications
You must be signed in to change notification settings - Fork 0
/
setgo-api.go
139 lines (114 loc) · 3.18 KB
/
setgo-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
package main
import (
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"setgo-api/structs"
"setgo-api/utilities"
_ "github.com/go-sql-driver/mysql"
)
// .env variables
var setAppUrl string
var cacheFile string
var subCatFile string
var helpTemplate string
var favIcon string
var url string
var subCats []structs.Subcat
func main() {
setAppUrl = utilities.GetConfigValue("SETAPP_URL")
cacheFile = utilities.GetConfigValue("CACHE_FILE")
subCatFile = utilities.GetConfigValue("SUBCAT_FILE")
helpTemplate = utilities.GetConfigValue("HELP_TEMPLATE")
url = utilities.GetConfigValue("SERVER_URL")
favIcon = utilities.GetConfigValue("FAVICON")
subCats, _ = utilities.LoadSubCats(subCatFile)
router := gin.Default()
router.LoadHTMLFiles(helpTemplate)
router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "helpTemplate", map[string]interface{}{}) })
router.StaticFile("/favicon.ico", favIcon)
router.GET("/apps", GetApps)
router.GET("/apps/:id", GetApps)
router.GET("/cats", GetAllCategories)
router.GET("/cats/:cat", GetByCategory)
router.GET("/subcats", GetAllSubCategories)
router.GET("/search/:query", GetBySearchTerm)
router.Run(url)
}
func GetApps(c *gin.Context) {
apps, _ := utilities.GetData(setAppUrl, cacheFile)
id := c.Param("id")
if id == "" {
c.IndentedJSON(http.StatusOK, apps)
return
} else {
for _, a := range apps {
reqID, _ := strconv.Atoi(id)
if reqID == a.Id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"error": "app id " + id + " was not found"})
return
}
}
func GetAllCategories(c *gin.Context) {
var catList []string
apps, _ := utilities.GetData(setAppUrl, cacheFile)
for _, a := range apps {
catList = append(catList, a.Categories...)
}
catList = utilities.RemoveDuplicate[string](catList)
c.IndentedJSON(http.StatusOK, catList)
}
func GetAllSubCategories(c *gin.Context) {
c.IndentedJSON(http.StatusOK, subCats)
}
func GetByCategory(c *gin.Context) {
var appList []structs.App
apps, _ := utilities.GetData(setAppUrl, cacheFile)
cat := c.Param("cat")
if cat == "" {
c.IndentedJSON(http.StatusOK, apps)
return
} else {
for _, a := range apps {
for _, currCat := range a.Categories {
if strings.EqualFold(cat, currCat) {
appList = append(appList, a)
}
}
}
if len(appList) < 1 {
c.IndentedJSON(http.StatusNotFound, gin.H{"error": "category " + cat + " was not found"})
} else {
c.IndentedJSON(http.StatusOK, appList)
return
}
}
}
func GetBySearchTerm(c *gin.Context) {
var appList []structs.App
apps, _ := utilities.GetData(setAppUrl, cacheFile)
query := strings.ToLower(c.Param("query"))
if query == "" {
c.IndentedJSON(http.StatusNotFound, gin.H{"error": "no search term provided"})
return
} else {
for _, a := range apps {
appName := strings.ToLower(a.Name)
appDesc := strings.ToLower(a.Description)
if strings.Contains(appName, query) || strings.Contains(appDesc, query) {
appList = append(appList, a)
}
}
if len(appList) < 1 {
c.IndentedJSON(http.StatusNotFound, gin.H{"error": "no results found for query '" + c.Param("query") + "'"})
} else {
c.IndentedJSON(http.StatusOK, appList)
return
}
}
}