-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
54 lines (44 loc) · 796 Bytes
/
utils.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
package amqp
import (
"strconv"
"math/rand"
)
const (
amqpHost = "localhost"
amqpPort = "5672"
)
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
func correlationId(l int) string {
bytes := make([]byte, l)
for i := 0; i < l; i++ {
bytes[i] = byte(randInt(65, 90))
}
return string(bytes)
}
func buildUrl(cfg Config) string {
amqpUrl := "amqp://"
if cfg.User != "" {
amqpUrl += cfg.User
if cfg.Password != "" {
amqpUrl += ":" + cfg.Password
}
amqpUrl += "@"
}
if cfg.Host == "" {
amqpUrl += amqpHost
} else {
amqpUrl += cfg.Host
}
port := strconv.Itoa(cfg.Port)
if port == "" || port == "0" {
port = amqpPort
} else {
amqpUrl += ":" + port
}
if cfg.VirtualHost != "" {
amqpUrl += "/" + cfg.VirtualHost
}
return amqpUrl
}