-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
update.go
94 lines (77 loc) · 2.45 KB
/
update.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
package market
import (
"net/http"
"encoding/json"
"github.com/azukaar/cosmos-server/src/utils"
"time"
)
type appDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Url string `json:"url"`
LongDescription string `json:"longDescription"`
Tags []string `json:"tags"`
Repository string `json:"repository"`
Image string `json:"image"`
Screenshots []string `json:"screenshots"`
Icon string `json:"icon"`
Compose string `json:"compose"`
SupportedArchitectures []string `json:"supported_architectures"`
}
type marketDefinition struct {
Showcase []appDefinition `json:"showcase"`
All []appDefinition `json:"all"`
Source string `json:"source"`
}
type marketCacheObject struct {
Url string `json:"url"`
Name string `json:"name"`
LastUpdate time.Time `json:"lastUpdate"`
Results marketDefinition `json:"results"`
}
var currentMarketcache []marketCacheObject
func GetCachedMarket() []marketCacheObject {
return currentMarketcache
}
func updateCache(w http.ResponseWriter, req *http.Request) error {
for index, cachedMarket := range currentMarketcache {
if cachedMarket.LastUpdate.Add(time.Hour * 12).Before(time.Now()) {
utils.Log("MarketUpdate: Updating market " + cachedMarket.Name)
// fetch market.url
resp, err := http.Get(cachedMarket.Url)
if err != nil {
utils.Error("MarketUpdate: Error while fetching market" + cachedMarket.Url, err)
// utils.HTTPError(w, "Market Get Error " + cachedMarket.Url, http.StatusInternalServerError, "MK001")
continue
}
defer resp.Body.Close()
// parse body
var result marketDefinition
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
utils.Error("MarketUpdate: Error while parsing market" + cachedMarket.Url, err)
// utils.HTTPError(w, "Market Get Error " + cachedMarket.Url, http.StatusInternalServerError, "MK003")
continue
}
result.Source = cachedMarket.Url
if cachedMarket.Name != "cosmos-cloud" {
result.Showcase = []appDefinition{}
}
cachedMarket.Results = result
cachedMarket.LastUpdate = time.Now()
utils.TriggerEvent(
"cosmos.market.update",
"Market updated",
"success",
"",
map[string]interface{}{
"market": cachedMarket.Name,
"numberOfApps": len(result.All),
})
utils.Log("MarketUpdate: Updated market " + result.Source + " with " + string(len(result.All)) + " results")
// save to cache
currentMarketcache[index] = cachedMarket
}
}
return nil
}