-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg_helper.go
167 lines (148 loc) · 3.61 KB
/
cfg_helper.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
package testhelpers
import (
"bytes"
"github.com/soldatov-s/accp/internal/cfg"
"github.com/soldatov-s/accp/internal/httpproxy"
"github.com/soldatov-s/accp/internal/introspection"
"github.com/soldatov-s/accp/internal/logger"
"github.com/spf13/viper"
)
const (
LoggerConfig = `
logger:
nocoloredoutput: true
withtrace: false
level: debug
`
IntrospectorHost = `localhost:8001`
IntrospectorDSN = `http://` + IntrospectorHost
IntropsectorConfig = `
introspector:
dsn: ` + IntrospectorDSN + `
endpoint: /oauth2/introspect
contenttype: application/x-www-form-urlencoded
method: POST
validmarker: '"active":true'
bodytemplate: "token_type_hint=access_token&token={{.Token}}"
cookiename: ["access-token"]
queryparamname: ["access_token"]
poolsize: 50
pooltimeout: 10s
`
ProxyConfig = `proxy:
listen: 0.0.0.0:9000
hydration:
requestid: true
routes:
/api/v1/:
parameters:
introspect: true
introspecthydration: plaintext # add result of introspection as plaintext in header
dsn: http://localhost:9090
pool:
size: 20
timeout: 10s
routekey: V1 # rabbitmq routekey
routes:
# subroutes
users:
parameters:
limits:
token:
counter: 1
pt: 3s
ip:
counter: 1
pt: 3s
deviceid:
header: [device-id]
cookie: [device-id]
counter: 1
pt: 1m # cache auto update period
refresh:
count: 3
time: 10s
cache:
memory:
ttl: 2s
external:
keyprefix: users_
ttl: 30s
excluded:
- search
/api/v1/users:
parameters:
dsn: http://localhost:9091
introspect: true
introspecthydration: plaintext # add result of introspection as plaintext in header
pool:
size: 20
timeout: 10s
routkey: USERS
limits:
token:
counter: 1000
pt: 1m
ip:
counter: 1000
pt: 1m
deviceid:
header: device-id
cookie: device-id
counter: 1000
pt: 1m
refresh:
count: 100
time: 2m
cache:
memory:
ttl: 2s
external:
keyprefix: users_
ttl: 30s
`
)
func LoadTestYAML() error {
viper.SetConfigType("yaml")
if err := viper.MergeConfig(bytes.NewBuffer([]byte(LoggerConfig))); err != nil {
return err
}
if err := viper.MergeConfig(bytes.NewBuffer([]byte(IntropsectorConfig))); err != nil {
return err
}
if err := viper.MergeConfig(bytes.NewBuffer([]byte(ProxyConfig))); err != nil {
return err
}
return nil
}
func LoadTestConfigLogger() (*logger.Config, error) {
var lc *logger.Config
if err := viper.UnmarshalKey("logger", &lc); err != nil {
return nil, err
}
return lc, nil
}
func LoadTestConfigIntrospector() (*introspection.Config, error) {
var ic *introspection.Config
if err := viper.UnmarshalKey("introspector", &ic); err != nil {
return nil, err
}
return ic, nil
}
func LoadTestConfigProxy() (*httpproxy.Config, error) {
var pc *httpproxy.Config
if err := viper.UnmarshalKey("proxy", &pc); err != nil {
return nil, err
}
return pc, nil
}
func LoadTestConfig() (*cfg.Config, error) {
c := &cfg.Config{}
if err := LoadTestYAML(); err != nil {
return nil, err
}
if err := viper.Unmarshal(c); err != nil {
return nil, err
}
return c, nil
}