forked from caddyserver/cache-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.go
175 lines (166 loc) · 5.56 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
package httpcache
import (
"fmt"
"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.Nuts.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.Nuts.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 := ""
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(),
)
}
}
}