This repository has been archived by the owner on Jun 14, 2018. It is now read-only.
/
hndl-store-list.go
86 lines (74 loc) · 1.64 KB
/
hndl-store-list.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
package main
import (
"encoding/json"
"mole/conf"
"log"
"net/http"
"os"
"path"
"path/filepath"
"sync"
)
func init() {
addHandler(handler{
pattern: "/store",
method: "GET",
fn: storeList,
auth: true,
ro: true,
})
}
var listCache []listItem
var listCacheLock sync.Mutex
type listItem struct {
Name string
Description string
Hosts []string
Version float64
Features uint32
}
func storeList(rw http.ResponseWriter, req *http.Request) {
defer listCacheLock.Unlock()
listCacheLock.Lock()
if listCache == nil {
files, err := filepath.Glob(storeDir + "/data/*.ini")
if err != nil {
rw.WriteHeader(500)
rw.Write([]byte(err.Error()))
return
}
for _, file := range files {
item := listItem{
Name: path.Base(file[:len(file)-4]),
}
f, err := os.Open(file)
if err != nil {
log.Printf("Warning: %q: %s", file, err)
item.Features = conf.FeatureError
item.Description = "- unreadable -"
listCache = append(listCache, item)
continue
}
cfg, err := conf.Load(f)
f.Close()
if err != nil {
log.Printf("Warning: %q: %s", file, err)
item.Features = conf.FeatureError
item.Description = "- parse error -"
listCache = append(listCache, item)
continue
}
var hosts []string
for _, h := range cfg.Hosts {
hosts = append(hosts, h.Name)
}
item.Features = cfg.FeatureFlags()
item.Description = cfg.General.Description
item.Hosts = hosts
item.Version = float64(cfg.General.Version) / 100
listCache = append(listCache, item)
}
}
rw.Header().Set("Content-Type", "application/json")
json.NewEncoder(rw).Encode(listCache)
}