forked from phachon/fasthttpsession
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
84 lines (66 loc) · 1.74 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
package postgres
import (
"fmt"
"net/url"
)
// session postgres config
type Config struct {
// The host to connect to. Values that start with / are for unix domain sockets. (default is localhost)
Host string
// The port to bind to. (default is 5432)
Port int64
// postgres user to sign in as
Username string
// postgres user's password
Password string
// Maximum wait for connection, in seconds. Zero or
// not specified means wait indefinitely.
ConnTimeout int64
// name of the database to connect to
Database string
// session table name
TableName string
// postgres max free idle
SetMaxIdleConn int
// postgres 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 int64, username string, password string, dbName string, tableName string) (cf *Config) {
cf = NewDefaultConfig()
cf.Host = host
cf.Port = port
cf.Username = username
cf.Password = password
cf.Database = dbName
cf.TableName = tableName
return
}
func NewDefaultConfig() *Config {
return &Config{
Host: "127.0.0.1",
Port: 5432,
Username: "root",
Password: "",
ConnTimeout: 3000,
Database: "test",
TableName: "test",
SetMaxOpenConn: 500,
SetMaxIdleConn: 50,
}
}
func (pc *Config) getPostgresDSN() string {
return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?connect_timeout=%d&sslmode=disable",
url.QueryEscape(pc.Username),
pc.Password,
url.QueryEscape(pc.Host),
pc.Port,
url.QueryEscape(pc.Database),
pc.ConnTimeout)
}
func (pc *Config) Name() string {
return ProviderName
}