From 952f39ddf25fae5ea302df9b8a101ceaaaf25e88 Mon Sep 17 00:00:00 2001 From: bigeagle Date: Tue, 3 Feb 2015 23:03:47 +0800 Subject: [PATCH] add option to disable redis --- godns.conf | 1 + hosts.go | 20 ++++++++++++++++++-- settings.go | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/godns.conf b/godns.conf index 5234a62..ec06255 100644 --- a/godns.conf +++ b/godns.conf @@ -17,6 +17,7 @@ timeout = 5 # 5 seconds domain-server-file = "/etc/godns.d/servers" [redis] +enable = true host = "127.0.0.1" port = 6379 db = 0 diff --git a/hosts.go b/hosts.go index 7390e76..2af165c 100644 --- a/hosts.go +++ b/hosts.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "errors" "net" "os" "regexp" @@ -17,8 +18,14 @@ type Hosts struct { func NewHosts(hs HostsSettings, rs RedisSettings) Hosts { fileHosts := &FileHosts{hs.HostsFile} - redis := &redis.Client{Addr: rs.Addr(), Db: rs.DB, Password: rs.Password} - redisHosts := &RedisHosts{redis, hs.RedisKey} + + var rc *redis.Client + if rs.Enable { + rc = &redis.Client{Addr: rs.Addr(), Db: rs.DB, Password: rs.Password} + } else { + rc = nil + } + redisHosts := &RedisHosts{rc, hs.RedisKey} hosts := Hosts{fileHosts.GetAll(), redisHosts} return hosts @@ -69,17 +76,26 @@ type RedisHosts struct { } func (r *RedisHosts) GetAll() map[string]string { + if r.redis == nil { + return map[string]string{} + } var hosts = make(map[string]string) r.redis.Hgetall(r.key, hosts) return hosts } func (r *RedisHosts) Get(domain string) (ip string, ok bool) { + if r.redis == nil { + return "", false + } b, err := r.redis.Hget(r.key, domain) return string(b), err == nil } func (r *RedisHosts) Set(domain, ip string) (bool, error) { + if r.redis == nil { + return false, errors.New("Redis not enabled") + } return r.redis.Hset(r.key, domain, []byte(ip)) } diff --git a/settings.go b/settings.go index 0660532..1481146 100644 --- a/settings.go +++ b/settings.go @@ -36,6 +36,7 @@ type DNSServerSettings struct { } type RedisSettings struct { + Enable bool Host string Port int DB int