Skip to content

Instantly share code, notes, and snippets.

@reterVision
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reterVision/517a4d822cdd94f1c47b to your computer and use it in GitHub Desktop.
Save reterVision/517a4d822cdd94f1c47b to your computer and use it in GitHub Desktop.
Test Rest API + Redis usage in Go
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/garyburd/redigo/redis"
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
)
type Message struct {
Body string
}
func campaign_list(c redis.Conn) map[string]interface{}{
response, err := redis.String(c.Do("GET", "test:campaigns"))
if err != nil {
log.Fatal(err)
}
test_resp := map[string]interface{}{}
response = strings.Replace(response, "\\", "", -1)
err = json.Unmarshal([]byte(response), &test_resp)
if err != nil {
panic(err)
}
return test_resp
}
func main() {
handler := rest.ResourceHandler{}
c, err := redis.Dial("tcp", ":6379")
if err != nil {
log.Fatal(err)
}
handler.SetRoutes(
&rest.Route{"GET", "/message", func(w rest.ResponseWriter, req * rest.Request) {
w.WriteJson(campaign_list(c))
}},
)
fmt.Println("Server starting at 127.0.0.1:9002")
http.ListenAndServe(":9002", &handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment