func ListRangeHandler(rw http.ResponseWriter, req *http.Request) { key := mux.Vars(req)["key"] list := simpleredis.NewList(pool, key) members := HandleError(list.GetAll()).([]string) membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte) rw.Write(membersJSON) }
func ListPushHandler(rw http.ResponseWriter, req *http.Request) { key := mux.Vars(req)["key"] value := mux.Vars(req)["value"] list := simpleredis.NewList(pool, key) HandleError(nil, list.Add(value)) ListRangeHandler(rw, req) }
func NewIPEngine(state pinterface.IUserState) *IPEngine { // Create a RedisList for storing IP adresses ips := simpleredis.NewList(state.Pool(), "IPs") ipEngine := new(IPEngine) ipEngine.data = ips ipEngine.state = state return ipEngine }
func ListPushHandler(rw http.ResponseWriter, req *http.Request) { println("ListPushHandler") /** * Expect a key and value as input. * */ key := mux.Vars(req)["key"] value := mux.Vars(req)["value"] println("New list " + key + " " + value) list := simpleredis.NewList(pool, key) HandleError(nil, list.Add(value)) ListRangeHandler(rw, req) }
func ListRangeHandler(rw http.ResponseWriter, req *http.Request) { members, err := simpleredis.NewList(pool, mux.Vars(req)["key"]).GetAll() if err != nil { panic(err) } membersJSON, err := json.MarshalIndent(members, "", " ") if err != nil { panic(err) } rw.WriteHeader(200) rw.Header().Set("Content-Type", "application/json") rw.Write([]byte(membersJSON)) }
/** * REST * input: key * * Writes all members to JSON. */ func ListRangeHandler(rw http.ResponseWriter, req *http.Request) { println("ListRangeHandler") key := mux.Vars(req)["key"] list := simpleredis.NewList(pool, key) //members := HandleError(list.GetAll()).([]string) members := HandleError(list.GetLastN(4)).([]string) print(members) membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte) print("RETURN MEMBERS = " + string(membersJSON)) rw.Write(membersJSON) }
func main() { // Check if the redis service is up if err := simpleredis.TestConnection(); err != nil { log.Fatalln("Could not connect to Redis. Is the service up and running?") } // Use instead for testing if a different host/port is up. // simpleredis.TestConnectionHost("localhost:6379") // Create a connection pool, connect to the given redis server pool := simpleredis.NewConnectionPool() // Use this for connecting to a different redis host/port // pool := simpleredis.NewConnectionPoolHost("localhost:6379") // For connecting to a different redis host/port, with a password // pool := simpleredis.NewConnectionPoolHost("password@redishost:6379") // Close the connection pool right after this function returns defer pool.Close() // Create a list named "greetings" list := simpleredis.NewList(pool, "greetings") // Add "hello" to the list, check if there are errors if list.Add("hello") != nil { log.Fatalln("Could not add an item to list!") } // Get the last item of the list if item, err := list.GetLast(); err != nil { log.Fatalln("Could not fetch the last item from the list!") } else { log.Println("The value of the stored item is:", item) } // Remove the list if list.Remove() != nil { log.Fatalln("Could not remove the list!") } }
func main() { var connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT") if connection == ":" { print("WARNING ::: If in kube, this is a failure: Missing env variable REDISMASTER_SERVICE_HOST") print("WARNING ::: Attempting to connect redis localhost.") connection = "127.0.0.1:6379" } else { print("Found redis master host " + os.Getenv("REDISMASTER_SERVICE_PORT")) connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT") } println("Now connecting to : " + connection) /** * Create a connection pool. ?The pool pointer will otherwise * not be of any use.?https://gist.github.com/jayunit100/1d00e6d343056401ef00 */ pool = simpleredis.NewConnectionPoolHost(connection) println("Connection pool established : " + connection) defer pool.Close() r := mux.NewRouter() println("Router created ") /** * Define a REST path. * - The parameters (key) can be accessed via mux.Vars. * - The Methods (GET) will be bound to a handler function. */ r.Path("/info").Methods("GET").HandlerFunc(InfoHandler) r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler) r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler) r.Path("/llen").Methods("GET").HandlerFunc(LLENHandler) //for dev environment, the site is one level up... r.PathPrefix("/").Handler(http.FileServer(http.Dir(pathToStaticContents()))) r.Path("/env").Methods("GET").HandlerFunc(EnvHandler) list := simpleredis.NewList(pool, "k8petstore") HandleError(nil, list.Add("jayunit100")) HandleError(nil, list.Add("tstclaire")) HandleError(nil, list.Add("rsquared")) // Verify that this is 3 on startup. infoL := HandleError(pool.Get(0).Do("LLEN", "k8petstore")).(int64) fmt.Printf("\n=========== Starting DB has %d elements \n", infoL) if infoL < 3 { print("Not enough entries in DB. something is wrong w/ redis querying") print(infoL) panic("Failed ... ") } println("=========== Now launching negroni...this might take a second...") n := negroni.Classic() n.UseHandler(r) n.Run(":3000") println("Done ! Web app is now running.") }