Last active
August 29, 2015 14:01
-
-
Save reterVision/517a4d822cdd94f1c47b to your computer and use it in GitHub Desktop.
Test Rest API + Redis usage in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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