-
Notifications
You must be signed in to change notification settings - Fork 24
/
dispatch.go
207 lines (196 loc) · 6.41 KB
/
dispatch.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package httpcache
import (
"fmt"
"os"
"strings"
"github.com/caddyserver/caddy/v2"
)
func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) {
if s.Configuration.DefaultCache.Badger.Found {
e := dispatchStorage(ctx, "badger", s.Configuration.DefaultCache.Badger, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during Badger init, did you include the Badger storage (--with github.com/darkweak/storages/badger/caddy)? %v", e)
} else {
badger := s.Configuration.DefaultCache.Badger
dir := ""
vdir := ""
if c := badger.Configuration; c != nil {
p, ok := c.(map[string]interface{})
if ok {
if d, ok := p["Dir"]; ok {
dir = fmt.Sprint(d)
vdir = fmt.Sprint(d)
}
if d, ok := p["ValueDir"]; ok {
vdir = fmt.Sprint(d)
}
}
}
s.Configuration.DefaultCache.Badger.Uuid = fmt.Sprintf(
"BADGER-%s-%s-%s",
dir,
vdir,
s.Configuration.DefaultCache.GetStale(),
)
}
}
if s.Configuration.DefaultCache.Etcd.Found {
e := dispatchStorage(ctx, "etcd", s.Configuration.DefaultCache.Etcd, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during Etcd init, did you include the Etcd storage (--with github.com/darkweak/storages/etcd/caddy)? %v", e)
} else {
etcd := s.Configuration.DefaultCache.Etcd
endpoints := etcd.URL
username := ""
password := ""
if c := etcd.Configuration; c != nil {
p, ok := c.(map[string]interface{})
if ok {
if d, ok := p["Endpoints"]; ok {
endpoints = fmt.Sprint(d)
}
if d, ok := p["Username"]; ok {
username = fmt.Sprint(d)
}
if d, ok := p["Password"]; ok {
password = fmt.Sprint(d)
}
}
}
s.Configuration.DefaultCache.Etcd.Uuid = fmt.Sprintf(
"ETCD-%s-%s-%s-%s",
endpoints,
username,
password,
s.Configuration.DefaultCache.GetStale(),
)
}
}
if s.Configuration.DefaultCache.Nats.Found {
e := dispatchStorage(ctx, "nats", s.Configuration.DefaultCache.Nats, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during Nats init, did you include the Nats storage (--with github.com/darkweak/storages/nats/caddy)? %v", e)
} else {
s.Configuration.DefaultCache.Nats.Uuid = fmt.Sprintf("NATS-%s-%s", s.Configuration.DefaultCache.Nats.URL, s.Configuration.DefaultCache.GetStale())
}
}
if s.Configuration.DefaultCache.Nuts.Found {
e := dispatchStorage(ctx, "nuts", s.Configuration.DefaultCache.Nuts, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during Nuts init, did you include the Nuts storage (--with github.com/darkweak/storages/nuts/caddy)? %v", e)
} else {
nuts := s.Configuration.DefaultCache.Nuts
dir := "/tmp/souin-nuts"
if c := nuts.Configuration; c != nil {
p, ok := c.(map[string]interface{})
if ok {
if d, ok := p["Dir"]; ok {
dir = fmt.Sprint(d)
}
}
} else if nuts.Path != "" {
dir = nuts.Path
}
s.Configuration.DefaultCache.Nuts.Uuid = fmt.Sprintf("NUTS-%s-%s", dir, s.Configuration.DefaultCache.GetStale())
}
}
if s.Configuration.DefaultCache.Olric.Found {
e := dispatchStorage(ctx, "olric", s.Configuration.DefaultCache.Olric, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during Olric init, did you include the Olric storage (--with github.com/darkweak/storages/olric/caddy)? %v", e)
} else {
s.Configuration.DefaultCache.Olric.Uuid = fmt.Sprintf("OLRIC-%s-%s", s.Configuration.DefaultCache.Olric.URL, s.Configuration.DefaultCache.GetStale())
}
}
if s.Configuration.DefaultCache.Otter.Found {
e := dispatchStorage(ctx, "otter", s.Configuration.DefaultCache.Otter, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during Otter init, did you include the Otter storage (--with github.com/darkweak/storages/otter/caddy)? %v", e)
} else {
s.Configuration.DefaultCache.Otter.Uuid = fmt.Sprintf("OTTER-%s", s.Configuration.DefaultCache.GetStale())
}
}
if s.Configuration.DefaultCache.Redis.Found {
e := dispatchStorage(ctx, "redis", s.Configuration.DefaultCache.Redis, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during Redis init, did you include the Redis storage (--with github.com/darkweak/storages/redis/caddy or github.com/darkweak/storages/go-redis/caddy)? %v", e)
} else {
redis := s.Configuration.DefaultCache.Redis
address := redis.URL
username := ""
dbname := "0"
cname := "souin-redis"
if c := redis.Configuration; c != nil {
p, ok := c.(map[string]interface{})
if ok {
// shared between go-redis and rueidis
if d, ok := p["Username"]; ok {
username = fmt.Sprint(d)
}
if d, ok := p["ClientName"]; ok {
cname = fmt.Sprint(d)
}
// rueidis
if d, ok := p["InitAddress"]; ok {
elements := make([]string, 0)
for _, elt := range d.([]interface{}) {
elements = append(elements, elt.(string))
}
address = strings.Join(elements, ",")
}
if d, ok := p["SelectDB"]; ok {
dbname = fmt.Sprint(d)
}
// go-redis
if d, ok := p["Addrs"]; ok {
elements := make([]string, 0)
for _, elt := range d.([]interface{}) {
elements = append(elements, elt.(string))
}
address = strings.Join(elements, ",")
}
if d, ok := p["DB"]; ok {
dbname = fmt.Sprint(d)
}
}
}
s.Configuration.DefaultCache.Redis.Uuid = fmt.Sprintf(
"REDIS-%s-%s-%s-%s-%s",
address,
username,
dbname,
cname,
s.Configuration.DefaultCache.GetStale(),
)
}
}
if s.Configuration.DefaultCache.SimpleFS.Found {
e := dispatchStorage(ctx, "simplefs", s.Configuration.DefaultCache.SimpleFS, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during SimpleFS init, did you include the SimpleFS storage (--with github.com/darkweak/storages/simplefs/caddy)? %v", e)
} else {
simplefs := s.Configuration.DefaultCache.SimpleFS
path := simplefs.Path
size := "0"
if c := simplefs.Configuration; c != nil {
p, ok := c.(map[string]interface{})
if ok {
if d, ok := p["path"]; path == "" && ok {
path = fmt.Sprint(d)
}
if d, ok := p["size"]; ok {
size = fmt.Sprint(d)
}
}
}
if path == "" {
path, _ = os.Getwd()
}
s.Configuration.DefaultCache.SimpleFS.Uuid = fmt.Sprintf(
"SIMPLEFS-%s-%s",
path,
size,
)
}
}
}