-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
index.go
75 lines (64 loc) · 1.79 KB
/
index.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
package market
import (
"net/http"
"encoding/json"
"fmt"
"github.com/azukaar/cosmos-server/src/utils"
)
type marketGetResult struct {
Showcase []appDefinition `json:"showcase"`
All map[string]interface{} `json:"all"`
}
func MarketGet(w http.ResponseWriter, req *http.Request) {
if utils.LoggedInOnly(w, req) != nil {
return
}
if(req.Method == "GET") {
config := utils.GetMainConfig()
configSourcesList := config.MarketConfig.Sources
configSources := map[string]bool{
"cosmos-cloud": true,
}
for _, source := range configSourcesList {
configSources[source.Name] = true
}
utils.Debug(fmt.Sprintf("MarketGet: Config sources: %v", configSources))
Init()
err := updateCache(w, req)
if err != nil {
utils.Error("MarketGet: Error while updating cache", err)
utils.HTTPError(w, "Error while updating cache", http.StatusInternalServerError, "MK002")
return
}
marketGetResult := marketGetResult{
All: make(map[string]interface{}),
Showcase: []appDefinition{},
}
for _, market := range currentMarketcache {
if !configSources[market.Name] {
continue
}
utils.Debug(fmt.Sprintf("MarketGet: Adding market %v", market.Name))
results := []appDefinition{}
for _, app := range market.Results.All {
results = append(results, app)
}
marketGetResult.All[market.Name] = results
}
if len(currentMarketcache) > 0 {
for _, market := range currentMarketcache {
if market.Name == "cosmos-cloud" {
marketGetResult.Showcase = market.Results.Showcase
}
}
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"data": marketGetResult,
})
} else {
utils.Error("MarketGet: Method not allowed" + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}