1
0
Fork 0
burlesque/server.go

125 lines
2.4 KiB
Go
Raw Normal View History

2014-07-10 12:19:39 +00:00
package main
import (
"encoding/json"
2014-07-29 06:57:07 +00:00
"fmt"
2014-07-10 12:19:39 +00:00
"io/ioutil"
"net/http"
"runtime"
2014-07-29 06:57:07 +00:00
"strconv"
2014-07-10 12:19:39 +00:00
"strings"
)
2014-09-09 08:20:40 +00:00
func startServer() {
port := fmt.Sprintf(":%d", config.port)
2014-07-29 06:56:46 +00:00
err := http.ListenAndServe(port, nil)
if 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-07-10 12:19:39 +00:00
info := make(map[string]map[string]uint)
for _, q := range queues {
2014-09-09 08:20:40 +00:00
info[q.name] = map[string]uint{
"messages": q.counter.distance(),
2014-07-10 12:19:39 +00:00
"subscriptions": 0,
}
}
2014-09-09 08:20:40 +00:00
for _, r := range pool.requests {
for _, q := range r.queues {
2014-07-10 12:19:39 +00:00
info[q]["subscriptions"]++
}
}
2014-07-16 19:18:54 +00:00
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) {
info := make(map[string]interface{})
2014-09-09 08:20:40 +00:00
info["version"] = version
2014-07-10 12:19:39 +00:00
info["goroutines"] = runtime.NumGoroutine()
s, err := storage.Status()
if err != nil {
2014-09-09 08:20:40 +00:00
alert(err, "Failed to get Kyoto Cabinet status")
}
2014-07-29 06:57:07 +00:00
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")
2014-07-29 06:59:37 +00:00
num, err := strconv.Atoi(tt[1])
if err != nil {
2014-07-29 06:57:07 +00:00
ks[tt[0]] = tt[1]
} else {
ks[tt[0]] = num
}
}
info["kyoto_cabinet"] = ks
2014-07-16 19:18:54 +00:00
jsn, _ := json.Marshal(info)
w.Write(jsn)
2014-07-10 12:19:39 +00:00
}
2014-09-09 08:20:40 +00:00
func publishHandler(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-09 08:20:40 +00:00
msg = message(r.FormValue("msg"))
2014-07-10 12:19:39 +00:00
}
2014-07-17 13:22:37 +00:00
qname := r.FormValue("queue")
2014-09-09 08:20:40 +00:00
ok := registerPublication(qname, msg)
2014-07-16 17:47:08 +00:00
if ok {
w.Write([]byte("OK"))
} else {
http.Error(w, "FAIL", 500)
}
2014-07-10 12:19:39 +00:00
}
2014-09-09 08:20:40 +00:00
func subscriptionHandler(w http.ResponseWriter, r *http.Request) {
rch := make(chan response)
2014-07-16 17:50:05 +00:00
abort := make(chan bool, 1)
2014-09-09 08:20:40 +00:00
req := &request{
queues: strings.Split(r.FormValue("queues"), ","),
responseCh: rch,
abort: abort,
2014-07-10 12:19:39 +00:00
}
2014-09-09 08:20:40 +00:00
go registerSubscription(req)
2014-07-10 12:19:39 +00:00
disconnected := w.(http.CloseNotifier).CloseNotify()
finished := make(chan bool)
go func() {
select {
case <-disconnected:
2014-07-17 13:22:37 +00:00
close(rch)
2014-07-16 17:50:05 +00:00
abort <- true
2014-07-10 12:19:39 +00:00
case <-finished:
}
2014-09-09 08:20:40 +00:00
req.purge()
2014-07-10 12:19:39 +00:00
}()
2014-07-17 13:22:37 +00:00
res, ok := <-rch
if !ok {
2014-07-10 12:19:39 +00:00
return
}
2014-09-09 08:20:40 +00:00
w.Header().Set("Queue", res.queue)
w.Write(res.message)
2014-07-10 12:19:39 +00:00
finished <- true
}
2014-09-09 08:20:40 +00:00
func setupServer() {
http.HandleFunc("/status", statusHandler)
http.HandleFunc("/debug", debugHandler)
http.HandleFunc("/publish", publishHandler)
http.HandleFunc("/subscribe", subscriptionHandler)
2014-07-10 12:19:39 +00:00
}