-
Notifications
You must be signed in to change notification settings - Fork 8
/
uri.go
71 lines (61 loc) · 1.33 KB
/
uri.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
package main
import (
"fmt"
goredis "gopkg.in/redis.v4"
"log"
"strconv"
"strings"
)
func parseURI(host string) (server *Redis_Server, err error) {
if strings.HasPrefix(host, "redis://") {
server, err = parseRedisURI(host)
} else {
server, err = rhost_split(host)
}
return
}
func rhost_split(host string) (*Redis_Server, error) {
tokens := strings.Split(host, ":")
if len(tokens) < 2 {
log.Fatal("rhost_split: Needs <host:port[:dbnum:[pass]]>")
}
host = tokens[0]
port, err := strconv.Atoi(tokens[1])
if err != nil {
log.Fatal("rhost_split: port conversion error: ", err)
}
serv := new(Redis_Server)
serv.host = host
serv.port = port
len_tokens := len(tokens)
if len_tokens > 2 {
db, err := strconv.Atoi(tokens[2])
if err != nil {
log.Fatal("rhost_split: db conversion error: ", err)
}
serv.db = db
}
if len_tokens > 3 {
serv.pass = tokens[3]
}
return serv, nil
}
func rhost_copy(r *Redis_Server) (*Redis_Server, error) {
opts := &goredis.Options{
Addr: fmt.Sprintf("%s:%d", r.host, r.port),
Password: r.pass,
DB: r.db,
}
c := goredis.NewClient(opts)
rs := &Redis_Server{
client: c,
host: r.host,
port: r.port,
db: r.db,
pass: r.pass,
}
return rs, nil
}
func redisToString(s *Redis_Server) string {
return fmt.Sprintf("<redis://%s:%d?db=%d>", s.host, s.port, s.db)
}