Skip to content

Instantly share code, notes, and snippets.

@tristanwietsma
Created April 30, 2013 03:16
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 tristanwietsma/5486410 to your computer and use it in GitHub Desktop.
Save tristanwietsma/5486410 to your computer and use it in GitHub Desktop.
RESTful service interface example RESTful service example in Go. Parses URI of form "localhost:8080/key'/value" into Redis (publishes <value> to channel <key>).
package main
import (
"fmt"
"github.com/vmihailenco/redis"
"net/http"
"strings"
)
func handler(w http.ResponseWriter, r *http.Request, c *redis.Client) {
tokens := strings.Split(r.URL.Path[1:], "/")
if len(tokens) == 2 {
fmt.Fprintf(w, "%s -> %s", tokens[0], tokens[1])
c.Publish(tokens[0], tokens[1])
} else {
fmt.Fprintf(w, "Invalid.")
}
}
func main() {
client := redis.NewTCPClient("localhost:6379", "", -1)
defer client.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handler(w, r, client)
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment