2014-07-10 12:19:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-07-29 06:57:07 +00:00
|
|
|
"fmt"
|
2014-07-10 12:19:39 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2014-09-11 10:27:55 +00:00
|
|
|
|
|
|
|
"github.com/KosyanMedia/burlesque/hub"
|
2014-07-10 12:19:39 +00:00
|
|
|
)
|
|
|
|
|
2014-09-09 08:20:40 +00:00
|
|
|
func startServer() {
|
2014-09-11 10:27:55 +00:00
|
|
|
http.HandleFunc("/status", statusHandler)
|
|
|
|
http.HandleFunc("/debug", debugHandler)
|
|
|
|
http.HandleFunc("/publish", pubHandler)
|
|
|
|
http.HandleFunc("/subscribe", subHandler)
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(fmt.Sprintf(":%d", config.port), nil); err != nil {
|
2014-09-09 08:20:40 +00:00
|
|
|
alert(err, "Error starting server on port %d", config.port)
|
2014-07-29 06:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 08:20:40 +00:00
|
|
|
func statusHandler(w http.ResponseWriter, r *http.Request) {
|
2014-09-11 10:27:55 +00:00
|
|
|
// info := make(map[string]map[string]uint)
|
|
|
|
|
|
|
|
// for _, q := range queues {
|
|
|
|
// info[q.name] = map[string]uint{
|
|
|
|
// // "messages": q.counter.distance(),
|
|
|
|
// "subscriptions": 0,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for _, r := range pool.requests {
|
|
|
|
// for _, q := range r.queues {
|
|
|
|
// info[q]["subscriptions"]++
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// jsn, _ := json.Marshal(info)
|
|
|
|
// w.Write(jsn)
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 08:20:40 +00:00
|
|
|
func debugHandler(w http.ResponseWriter, r *http.Request) {
|
2014-09-11 10:27:55 +00:00
|
|
|
// info := make(map[string]interface{})
|
|
|
|
// info["version"] = version
|
|
|
|
// info["goroutines"] = runtime.NumGoroutine()
|
|
|
|
|
|
|
|
// s, err := storage.Status()
|
|
|
|
// if err != nil {
|
|
|
|
// alert(err, "Failed to get Kyoto Cabinet status")
|
|
|
|
// }
|
|
|
|
// s = s[:len(s)-1] // Removing trailing new line
|
|
|
|
|
|
|
|
// ks := make(map[string]interface{})
|
|
|
|
// tokens := strings.Split(s, "\n")
|
|
|
|
// for _, t := range tokens {
|
|
|
|
// tt := strings.Split(t, "\t")
|
|
|
|
// num, err := strconv.Atoi(tt[1])
|
|
|
|
// if err != nil {
|
|
|
|
// ks[tt[0]] = tt[1]
|
|
|
|
// } else {
|
|
|
|
// ks[tt[0]] = num
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// info["kyoto_cabinet"] = ks
|
|
|
|
|
|
|
|
// jsn, _ := json.Marshal(info)
|
|
|
|
// w.Write(jsn)
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 10:27:55 +00:00
|
|
|
func pubHandler(w http.ResponseWriter, r *http.Request) {
|
2014-07-10 12:19:39 +00:00
|
|
|
defer r.Body.Close()
|
|
|
|
msg, _ := ioutil.ReadAll(r.Body)
|
|
|
|
if len(msg) == 0 {
|
2014-09-11 10:27:55 +00:00
|
|
|
msg = []byte(r.FormValue("msg"))
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|
2014-09-11 10:27:55 +00:00
|
|
|
queue := r.FormValue("queue")
|
2014-07-10 12:19:39 +00:00
|
|
|
|
2014-09-11 10:27:55 +00:00
|
|
|
if ok := theHub.Pub(queue, msg); ok {
|
2014-07-16 17:47:08 +00:00
|
|
|
w.Write([]byte("OK"))
|
|
|
|
} else {
|
|
|
|
http.Error(w, "FAIL", 500)
|
|
|
|
}
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 10:27:55 +00:00
|
|
|
func subHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
result := make(chan hub.Result)
|
|
|
|
queues := strings.Split(r.FormValue("queues"), ",")
|
|
|
|
sub := hub.NewSubscription(queues, result)
|
2014-07-10 12:19:39 +00:00
|
|
|
|
|
|
|
disconnected := w.(http.CloseNotifier).CloseNotify()
|
2014-09-11 10:27:55 +00:00
|
|
|
finished := make(chan struct{})
|
2014-07-10 12:19:39 +00:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-disconnected:
|
2014-09-11 10:27:55 +00:00
|
|
|
sub.Close()
|
|
|
|
close(finished)
|
2014-07-10 12:19:39 +00:00
|
|
|
case <-finished:
|
|
|
|
}
|
|
|
|
}()
|
2014-09-11 10:27:55 +00:00
|
|
|
defer sub.Close()
|
2014-07-10 12:19:39 +00:00
|
|
|
|
2014-09-11 10:27:55 +00:00
|
|
|
theHub.Sub(sub)
|
|
|
|
res := <-result
|
2014-07-10 12:19:39 +00:00
|
|
|
|
2014-09-11 10:27:55 +00:00
|
|
|
w.Header().Set("Queue", res.Queue)
|
|
|
|
w.Write(res.Message)
|
2014-07-10 12:19:39 +00:00
|
|
|
|
2014-09-11 10:27:55 +00:00
|
|
|
finished <- struct{}{}
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|