forked from phachon/fasthttpsession
/
config.go
104 lines (82 loc) · 1.87 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
package mysql
import (
"fmt"
"net/url"
)
// session mysql config
type Config struct {
// mysql server host
Host string
// mysql server port
Port int
// mysql username
Username string
// mysql password
Password string
// mysql conn charset
Charset string
// mysql Collate
Collate string
// database name
Database string
// session table name
TableName string
// mysql conn timeout(s)
Timeout int
// mysql read timeout(s)
ReadTimeout int
// mysql write timeout(s)
WriteTimeout int
// mysql max free idle
SetMaxIdleConn int
// mysql max open idle
SetMaxOpenConn int
// session value serialize func
SerializeFunc func(data map[string]interface{}) ([]byte, error)
// session value unSerialize func
UnSerializeFunc func(data []byte) (map[string]interface{}, error)
}
func NewConfigWith(host string, port int, user, pass, dbName, tableName string) (cf *Config) {
cf = NewDefaultConfig()
cf.Host = host
cf.Port = port
cf.Username = user
cf.Password = pass
cf.Database = dbName
cf.TableName = tableName
return
}
func NewDefaultConfig() *Config {
return &Config{
Charset: "utf8",
Collate: "utf8_general_ci",
Database: "test",
TableName: "test",
Host: "127.0.0.1",
Port: 3306,
Username: "root",
Password: "",
Timeout: 3000,
ReadTimeout: 5000,
WriteTimeout: 5000,
SetMaxOpenConn: 500,
SetMaxIdleConn: 50,
}
}
// get mysql dsn
func (mc *Config) getMysqlDSN() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?timeout=%dms&readTimeout=%dms&writeTimeout=%dms&charset=%s&collation=%s",
url.QueryEscape(mc.Username),
mc.Password,
url.QueryEscape(mc.Host),
mc.Port,
url.QueryEscape(mc.Database),
mc.Timeout,
mc.ReadTimeout,
mc.WriteTimeout,
url.QueryEscape(mc.Charset),
url.QueryEscape(mc.Collate))
}
func (mc *Config) Name() string {
return ProviderName
}