This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
config.go
130 lines (113 loc) · 2.42 KB
/
config.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
// Configuration loading, reloading and setting
package db
import (
"log"
"github.com/bakape/meguca/config"
"github.com/bakape/meguca/templates"
"github.com/bakape/meguca/util"
r "github.com/dancannon/gorethink"
)
type boardConfUpdate struct {
Deleted bool
config.BoardConfigs
}
// Load configs from the database and update on each change
func loadConfigs() error {
cursor, err := GetMain("config").
Changes(r.ChangesOpts{IncludeInitial: true}).
Field("new_val").
Run(RSession)
if err != nil {
return err
}
read := make(chan config.Configs)
cursor.Listen(read)
initial := <-read
// Reload configuration on any change in the database
if !IsTest {
go func() {
for {
if err := updateConfigs(<-read); err != nil {
log.Println(err)
}
}
}()
}
return config.Set(initial)
}
func loadBoardConfigs() error {
// First load all boards
var all []config.BoardConfigs
if err := All(r.Table("boards"), &all); err != nil {
return err
}
for _, conf := range all {
_, err := config.SetBoardConfigs(conf)
if err != nil {
return err
}
}
// Then start listening to updates. This will also contain the initial
// values, so we deduplicate server-side.
cursor, err := r.
Table("boards").
Changes(r.ChangesOpts{
IncludeInitial: true,
}).
Map(func(b r.Term) r.Term {
return r.Branch(
b.Field("new_val").Eq(nil),
map[string]interface{}{
"deleted": true,
"id": b.Field("old_val").Field("id"),
},
b.Field("new_val"),
)
}).
Run(RSession)
if err != nil {
return err
}
read := make(chan boardConfUpdate)
cursor.Listen(read)
if IsTest {
return nil
}
go func() {
for {
if err := updateBoardConfigs(<-read); err != nil {
log.Println(err)
}
}
}()
return nil
}
func updateConfigs(conf config.Configs) error {
if err := config.Set(conf); err != nil {
return util.WrapError("reloading configuration", err)
}
return recompileTemplates()
}
func updateBoardConfigs(u boardConfUpdate) error {
if u.Deleted {
config.RemoveBoard(u.ID)
return recompileTemplates()
}
changed, err := config.SetBoardConfigs(u.BoardConfigs)
if err != nil {
return util.WrapError("reloading board configuration", err)
}
if changed {
return recompileTemplates()
}
return nil
}
func recompileTemplates() error {
if IsTest {
return nil
}
if err := templates.Compile(); err != nil {
return util.WrapError("recompiling templates", err)
}
return nil
}