-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver_redis.go
54 lines (44 loc) · 1.34 KB
/
driver_redis.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
// To run:
// go get github.com/githubnemo/CompileDaemon
// CompileDaemon -command="./gervice"
package gosd
import "os"
import "fmt"
import "time"
import "strconv"
import redis "gopkg.in/redis.v3"
type DriverRedis struct {}
var RedisClient *redis.Client
func (this DriverRedis) Start(name, url string) string {
// start
redisDB,_ := strconv.Atoi(os.Getenv("gosdRedisDB"))
RedisClient = redis.NewClient(&redis.Options{
Addr: os.Getenv("gosdRedisAddr"),
Password: os.Getenv("gosdRedisPassword"), // no password set
DB: int64(redisDB), // use default DB
})
_, err := RedisClient.Ping().Result()
if err != nil {
fmt.Println("Error connecting with Redis.")
fmt.Println(err.Error())
return "standalone-" + name
}
// set
currentName := registerService(name, url)
return currentName
}
func (this DriverRedis) Get() (map[string]string, error) {
return RedisClient.HGetAllMap("gosd").Result()
}
func (this DriverRedis) Delete(currentName string) {
RedisClient.HDel("gosd", currentName)
}
func registerService(basicName, url string) string {
finalServiceName := ""
created := false
for created != true {
finalServiceName = basicName + "-" + time.Now().Format("20060102150405.99999999")
created,_ = RedisClient.HSetNX("gosd", finalServiceName, url).Result()
}
return finalServiceName
}