-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
261 lines (223 loc) · 5.8 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// MIT License
// Copyright (c) 2022 Leon Ding
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package gws
import (
"fmt"
"net"
"regexp"
"strings"
"time"
)
type (
store uint8
)
const (
ram store = iota // Session storage ram type
rds // Session storage rds type
def
prefix = "gws_id" // Default prefix
lifeTime = time.Duration(1800) * time.Second // Default session lifetime
)
var (
// Default option
defaultOption = option{
LifeTime: lifeTime,
CookieName: prefix,
Domain: "",
Path: "/",
HttpOnly: true,
Secure: true,
}
// DefaultRAMOptions default RAM config parameter option.
DefaultRAMOptions = &RAMOption{
option: defaultOption,
}
// NewRDSOptions default RDS config parameter option.
NewRDSOptions = func(ip string, port uint16, passwd string, opts ...func(*RDSOption)) *RDSOption {
var rdsopt RDSOption
rdsopt.option = defaultOption
rdsopt.Index = 6
rdsopt.Prefix = prefix
rdsopt.PoolSize = 10
rdsopt.Password = passwd
rdsopt.Address = fmt.Sprintf("%s:%v", ip, port)
for _, opt := range opts {
opt(&rdsopt)
}
return &rdsopt
}
// WithIndex set redis database number
WithIndex = func(number uint8) func(*RDSOption) {
return func(r *RDSOption) {
r.Index = number
}
}
// WithPoolSize set redis connection pool size
WithPoolSize = func(poolSize uint8) func(*RDSOption) {
return func(r *RDSOption) {
r.PoolSize = poolSize
}
}
// WithPrefix set redis key prefix
WithPrefix = func(prefix string) func(*RDSOption) {
return func(r *RDSOption) {
r.Prefix = prefix
}
}
// WithOpts set base option
WithOpts = func(opt Options) func(*RDSOption) {
return func(r *RDSOption) {
r.option = opt.option
}
}
)
// option type is default config parameter option.
type option struct {
LifeTime time.Duration `json:"life_time"`
CookieName string `json:"cookie_name"`
HttpOnly bool `json:"http_only"`
Path string `json:"path"`
Secure bool `json:"secure"`
Domain string `json:"domain"`
}
// Options type is default config parameter option.
type Options struct {
option
}
var (
WithLifeTime = func(d time.Duration) func(*Options) {
return func(o *Options) {
o.LifeTime = d
}
}
WithCookieName = func(cn string) func(*Options) {
return func(o *Options) {
o.CookieName = cn
}
}
WithPath = func(path string) func(*Options) {
return func(o *Options) {
o.Path = path
}
}
WithHttpOnly = func(b bool) func(*Options) {
return func(o *Options) {
o.HttpOnly = b
}
}
WithSecure = func(b bool) func(*Options) {
return func(o *Options) {
o.Secure = b
}
}
WithDomain = func(domain string) func(*Options) {
return func(o *Options) {
o.Domain = domain
}
}
)
// NewOptions Initialize default config.
func NewOptions(opts ...func(*Options)) Options {
var opt Options
opt.option = defaultOption
for _, v := range opts {
v(&opt)
}
return opt
}
// RAMOption is RAM storage config parameter option.
type RAMOption struct {
option
}
// RDSOption is Redis storage config parameter option.
type RDSOption struct {
option
Index uint8 `json:"db_index" `
Prefix string `json:"prefix" `
Address string `json:"address" `
Password string `json:"password" `
PoolSize uint8 `json:"pool_size" `
}
// Configure is session storage config parameter parser.
type Configure interface {
Parse() (cfg *Config)
}
// Config is session storage config parameter.
type Config struct {
store `json:"store,omitempty"`
*RDSOption
}
func (opt *Options) Parse() (cfg *Config) {
cfg = new(Config)
cfg.store = def
cfg.RDSOption = new(RDSOption)
cfg.RDSOption.option = opt.option
return verifyCfg(cfg)
}
func (opt *RAMOption) Parse() (cfg *Config) {
cfg = new(Config)
cfg.store = ram
cfg.RDSOption = new(RDSOption)
cfg.RDSOption.option = opt.option
return verifyCfg(cfg)
}
func (opt *RDSOption) Parse() (cfg *Config) {
cfg = new(Config)
cfg.store = rds
cfg.RDSOption = opt
return verifyCfg(cfg)
}
// Check the data
func verifyCfg(cfg *Config) *Config {
// General check
if cfg.CookieName == "" {
panic("cookie name is empty.")
}
if cfg.Path == "" {
panic("domain path is empty.")
}
if cfg.LifeTime <= 0 {
cfg.LifeTime = lifeTime
}
if cfg.store == ram || cfg.store == def {
return cfg
}
if cfg.Index > 16 {
cfg.Index = 6
}
if cfg.PoolSize <= 0 {
cfg.PoolSize = 10
}
if cfg.Prefix == "" {
cfg.Prefix = prefix
}
if cfg.Password == "" {
panic("remote server login passwd is empty.")
}
// Verification for specific storage
if net.ParseIP(strings.Split(cfg.Address, ":")[0]) == nil {
panic("remote ip address illegal.")
}
if matched, err := regexp.MatchString("^[0-9]*$", strings.Split(cfg.Address, ":")[1]); err == nil {
if !matched {
panic("remote server port illegal.")
}
}
debug.trace(cfg)
return cfg
}