Skip to content

Instantly share code, notes, and snippets.

@PirosB3
Created May 23, 2014 22:48
Show Gist options
  • Save PirosB3/b3af2dbc827b1e51852e to your computer and use it in GitHub Desktop.
Save PirosB3/b3af2dbc827b1e51852e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
zmq "github.com/alecthomas/gozmq"
"encoding/json"
)
func MakePhrase(s string) ([]byte, error) {
m := make(map[string]string)
m["text"] = s
return json.Marshal(m)
}
func main() {
in := make(chan(string))
out := make(chan(string))
go func() {
context, _ := zmq.NewContext()
socket, _ := context.NewSocket(zmq.REQ)
socket.Bind("tcp://*:9999")
for {
s := <- in
data, _ := MakePhrase(s)
socket.Send(data, 0)
response, _ := socket.Recv(0)
out <- string(response)
}
}()
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
s := r.FormValue("text")
if len(s) > 0 {
in <- s
o := <- out
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, o)
} else {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment