This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage.go
82 lines (67 loc) · 1.57 KB
/
storage.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
package main
import (
"encoding/json"
"os"
)
var (
u = make(users)
sMap = servers{serverMap: make(map[string]*server)}
)
func saveJSON(path string, data interface{}) error {
f, err := os.OpenFile("json/"+path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Error("error saving", path, err)
return err
}
if err = json.NewEncoder(f).Encode(data); err != nil {
log.Error("error saving", path, err)
return err
}
return nil
}
func loadJSON(path string, v interface{}) error {
f, err := os.OpenFile("json/"+path, os.O_RDONLY, 0600)
if err != nil {
log.Error("error loading", path, err)
return err
}
if err := json.NewDecoder(f).Decode(v); err != nil {
log.Error("error loading", path, err)
return err
}
return nil
}
func cleanup() {
for _, f := range []func() error{saveConfig, saveQueue, saveServers, saveUsers} {
if err := f(); err != nil {
log.Error("error cleaning up files", err)
}
}
log.Info("Done cleanup. Exiting.")
}
func loadConfig() error {
return loadJSON("config.json", conf)
}
func saveConfig() error {
return saveJSON("config.json", conf)
}
func loadServers() error {
sMap = servers{serverMap: make(map[string]*server)}
return loadJSON("servers.json", &sMap)
}
func saveServers() error {
return saveJSON("servers.json", sMap)
}
func loadUsers() error {
u = make(map[string]*user)
return loadJSON("users.json", &u)
}
func saveUsers() error {
return saveJSON("users.json", u)
}
func loadQueue() error {
return loadJSON("queue.json", &imageQueue)
}
func saveQueue() error {
return saveJSON("queue.json", imageQueue)
}