Make guestbook-go example actually use the redis-slave service that is set up for its reads.

This commit is contained in:
Jason Hunter 2015-04-10 17:54:41 -07:00
parent fff6cebb5f
commit 2ecb95b5b6

View File

@ -27,11 +27,14 @@ import (
"github.com/xyproto/simpleredis"
)
var pool *simpleredis.ConnectionPool
var (
masterPool *simpleredis.ConnectionPool
slavePool *simpleredis.ConnectionPool
)
func ListRangeHandler(rw http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["key"]
list := simpleredis.NewList(pool, key)
list := simpleredis.NewList(slavePool, key)
members := HandleError(list.GetAll()).([]string)
membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte)
rw.Write(membersJSON)
@ -40,13 +43,13 @@ func ListRangeHandler(rw http.ResponseWriter, req *http.Request) {
func ListPushHandler(rw http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["key"]
value := mux.Vars(req)["value"]
list := simpleredis.NewList(pool, key)
list := simpleredis.NewList(masterPool, key)
HandleError(nil, list.Add(value))
ListRangeHandler(rw, req)
}
func InfoHandler(rw http.ResponseWriter, req *http.Request) {
info := HandleError(pool.Get(0).Do("INFO")).([]byte)
info := HandleError(masterPool.Get(0).Do("INFO")).([]byte)
rw.Write(info)
}
@ -71,8 +74,10 @@ func HandleError(result interface{}, err error) (r interface{}) {
}
func main() {
pool = simpleredis.NewConnectionPoolHost("redis-master:6379")
defer pool.Close()
masterPool = simpleredis.NewConnectionPoolHost("redis-master:6379")
defer masterPool.Close()
slavePool = simpleredis.NewConnectionPoolHost("redis-slave:6379")
defer slavePool.Close()
r := mux.NewRouter()
r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)