1
0
Fork 0
burlesque/server/server.go

159 lines
3.5 KiB
Go
Raw Normal View History

2014-09-24 09:49:27 +00:00
package server
2014-07-10 12:19:39 +00:00
import (
2014-09-11 19:13:53 +00:00
"encoding/json"
2014-07-29 06:57:07 +00:00
"fmt"
2014-07-10 12:19:39 +00:00
"io/ioutil"
"net/http"
2015-01-25 10:24:07 +00:00
"os"
2014-09-24 10:13:08 +00:00
"runtime"
2014-07-10 12:19:39 +00:00
"strings"
2015-01-25 10:24:07 +00:00
"text/template"
2014-09-11 10:27:55 +00:00
2015-01-28 06:01:30 +00:00
"github.com/GeertJohan/go.rice"
2014-09-11 10:27:55 +00:00
"github.com/KosyanMedia/burlesque/hub"
2014-07-10 12:19:39 +00:00
)
2014-09-24 09:49:27 +00:00
type (
Server struct {
2015-01-28 06:01:30 +00:00
port int
hub *hub.Hub
dashboardTmpl string
2014-09-24 09:49:27 +00:00
}
)
2014-09-24 10:13:08 +00:00
const (
2015-01-25 10:24:07 +00:00
Version = "1.1.0"
2014-09-24 10:13:08 +00:00
)
2014-09-24 09:49:27 +00:00
func New(port int, h *hub.Hub) *Server {
s := Server{
port: port,
hub: h,
}
2015-01-28 06:01:30 +00:00
box := rice.MustFindBox("static")
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(box.HTTPBox())))
2014-09-24 09:49:27 +00:00
http.HandleFunc("/status", s.statusHandler)
http.HandleFunc("/debug", s.debugHandler)
http.HandleFunc("/publish", s.pubHandler)
http.HandleFunc("/subscribe", s.subHandler)
2014-09-24 15:37:33 +00:00
http.HandleFunc("/flush", s.flushHandler)
2015-01-25 10:24:07 +00:00
http.HandleFunc("/dashboard", s.dashboardHandler)
2014-09-24 09:49:27 +00:00
2015-01-28 06:01:30 +00:00
s.dashboardTmpl, _ = box.String("dashboard.tmpl")
2014-09-24 09:49:27 +00:00
return &s
}
2014-09-11 10:27:55 +00:00
2014-09-24 09:49:27 +00:00
func (s *Server) Start() {
port := fmt.Sprintf(":%d", s.port)
2014-09-11 19:00:19 +00:00
if err := http.ListenAndServe(port, nil); err != nil {
2014-09-11 10:30:15 +00:00
panic(err)
2014-07-29 06:56:46 +00:00
}
}
2014-09-24 09:49:27 +00:00
func (s *Server) statusHandler(w http.ResponseWriter, r *http.Request) {
2015-01-27 06:57:44 +00:00
var (
res = map[string]map[string]interface{}{}
info = s.hub.Info()
withRates = (r.FormValue("rates") != "")
)
for queue, meta := range info {
res[queue] = map[string]interface{}{}
for key, val := range meta {
res[queue][key] = val
}
if withRates {
inRate, outRate := s.hub.Rates(queue)
inHist, outHist := s.hub.RateHistory(queue)
res[queue]["in_rate"] = inRate
res[queue]["out_rate"] = outRate
res[queue]["in_rate_history"] = inHist
res[queue]["out_rate_history"] = outHist
}
}
2015-01-27 06:57:44 +00:00
jsn, _ := json.Marshal(res)
2014-09-25 13:23:59 +00:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2014-09-11 19:13:53 +00:00
w.Write(jsn)
2014-07-10 12:19:39 +00:00
}
2014-09-24 09:49:27 +00:00
func (s *Server) debugHandler(w http.ResponseWriter, r *http.Request) {
2014-09-24 10:13:08 +00:00
info := make(map[string]interface{})
info["version"] = Version
info["gomaxprocs"] = runtime.GOMAXPROCS(-1)
info["goroutines"] = runtime.NumGoroutine()
info["kyoto_cabinet"] = s.hub.StorageInfo()
jsn, _ := json.Marshal(info)
2014-09-25 13:23:59 +00:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2014-09-24 10:13:08 +00:00
w.Write(jsn)
2014-07-10 12:19:39 +00:00
}
2014-09-24 09:49:27 +00:00
func (s *Server) pubHandler(w http.ResponseWriter, r *http.Request) {
2014-07-10 12:19:39 +00:00
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-24 09:49:27 +00:00
if ok := s.hub.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-24 09:49:27 +00:00
func (s *Server) subHandler(w http.ResponseWriter, r *http.Request) {
2014-09-11 10:27:55 +00:00
queues := strings.Split(r.FormValue("queues"), ",")
2014-09-24 12:26:05 +00:00
sub := hub.NewSubscription(queues)
2014-07-10 12:19:39 +00:00
2014-09-11 10:27:55 +00:00
finished := make(chan struct{})
2014-09-11 19:00:19 +00:00
defer close(finished)
disconnected := w.(http.CloseNotifier).CloseNotify()
2014-07-10 12:19:39 +00:00
go func() {
select {
case <-disconnected:
case <-finished:
}
2014-09-24 12:26:05 +00:00
sub.Close()
2014-07-10 12:19:39 +00:00
}()
2014-09-24 09:49:27 +00:00
go s.hub.Sub(sub)
2014-07-10 12:19:39 +00:00
2014-09-24 12:26:05 +00:00
if res, ok := <-sub.Result(); ok {
w.Header().Set("Queue", res.Queue)
w.Write(res.Message)
}
2014-07-10 12:19:39 +00:00
}
2014-09-24 15:37:33 +00:00
func (s *Server) flushHandler(w http.ResponseWriter, r *http.Request) {
queues := strings.Split(r.FormValue("queues"), ",")
messages := s.hub.Flush(queues)
jsn, _ := json.Marshal(messages)
2014-09-25 13:23:59 +00:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2014-09-24 15:37:33 +00:00
w.Write(jsn)
}
2015-01-25 10:24:07 +00:00
func (s *Server) dashboardHandler(w http.ResponseWriter, r *http.Request) {
2015-01-26 08:07:23 +00:00
tmpl := template.New("dashboard")
2015-01-28 06:01:30 +00:00
tmpl, _ = tmpl.Parse(s.dashboardTmpl)
2015-01-25 10:24:07 +00:00
w.Header().Set("Content-Type", "text/html; charset=utf8")
hostname, _ := os.Hostname()
if hostname == "" {
hostname = "Unknown Host"
}
tmpl.ExecuteTemplate(w, "dashboard", map[string]interface{}{
"version": Version,
"hostname": hostname,
})
}