-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
90 lines (74 loc) · 1.34 KB
/
common.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
package cstr
import (
"fmt"
"net/url"
)
type Connecter interface {
Cstr() string
}
type Redis struct {
Host string
Port int
Db int
Name string `conf:"optional"`
}
func (r Redis) Cstr() string {
return ""
}
type Memcache struct {
Host string
Port int
}
func (m Memcache) Cstr() string {
return fmt.Sprintf("%s:%d",
m.Host, m.Port,
)
}
type Sqlite struct {
Path string
}
func (s Sqlite) Cstr() string {
return s.Path
}
type Mysql struct {
Host string
Port int
Db string
Username string
Password string
Timezone string `conf:"optional"`
}
func (m Mysql) Cstr() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true&loc=%s",
m.Username, m.Password,
m.Host, m.Port, m.Db,
url.QueryEscape(m.Timezone),
)
}
type Mongodb struct {
Host string
Port int
Db string
Username string `conf:"optional"`
Password string `conf:"optional"`
Replicas string `conf:"optional"`
Options string `conf:"optional"`
}
func (m Mongodb) Cstr() string {
replicas := m.Replicas
if replicas != "" {
replicas = "," + replicas
}
options := m.Options
if options != "" {
options = "?" + options
}
auth := ""
if m.Username != "" || m.Password != "" {
auth = m.Username + ":" + m.Password + "@"
}
return fmt.Sprintf("mongodb://%s%s:%d%s/%s%s",
auth, m.Host, m.Port, replicas,
m.Db, options,
)
}