burlesque/server/server.go

132 lines
2.8 KiB
Go
Raw Normal View History

2014-09-24 13:49:27 +04:00
package server
2014-07-10 19:19:39 +07:00
import (
2014-09-11 23:13:53 +04:00
"encoding/json"
2014-07-29 13:57:07 +07:00
"fmt"
2014-07-10 19:19:39 +07:00
"io/ioutil"
"net/http"
2015-01-25 17:24:07 +07:00
"os"
2014-09-24 14:13:08 +04:00
"runtime"
2014-07-10 19:19:39 +07:00
"strings"
2015-01-25 17:24:07 +07:00
"text/template"
2014-09-11 14:27:55 +04:00
"github.com/KosyanMedia/burlesque/hub"
2014-07-10 19:19:39 +07:00
)
2014-09-24 13:49:27 +04:00
type (
Server struct {
port int
hub *hub.Hub
}
)
2014-09-24 14:13:08 +04:00
const (
2015-01-25 17:24:07 +07:00
Version = "1.1.0"
2014-09-24 14:13:08 +04:00
)
2014-09-24 13:49:27 +04:00
func New(port int, h *hub.Hub) *Server {
s := Server{
port: port,
hub: h,
}
http.HandleFunc("/status", s.statusHandler)
http.HandleFunc("/debug", s.debugHandler)
http.HandleFunc("/publish", s.pubHandler)
http.HandleFunc("/subscribe", s.subHandler)
2014-09-24 19:37:33 +04:00
http.HandleFunc("/flush", s.flushHandler)
2015-01-25 17:24:07 +07:00
http.HandleFunc("/dashboard", s.dashboardHandler)
2014-09-24 13:49:27 +04:00
return &s
}
2014-09-11 14:27:55 +04:00
2014-09-24 13:49:27 +04:00
func (s *Server) Start() {
port := fmt.Sprintf(":%d", s.port)
2014-09-11 23:00:19 +04:00
if err := http.ListenAndServe(port, nil); err != nil {
2014-09-11 14:30:15 +04:00
panic(err)
2014-07-29 13:56:46 +07:00
}
}
2014-09-24 13:49:27 +04:00
func (s *Server) statusHandler(w http.ResponseWriter, r *http.Request) {
info := s.hub.Info()
2014-09-11 23:13:53 +04:00
jsn, _ := json.Marshal(info)
2014-09-25 17:23:59 +04:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2014-09-11 23:13:53 +04:00
w.Write(jsn)
2014-07-10 19:19:39 +07:00
}
2014-09-24 13:49:27 +04:00
func (s *Server) debugHandler(w http.ResponseWriter, r *http.Request) {
2014-09-24 14:13:08 +04: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 17:23:59 +04:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2014-09-24 14:13:08 +04:00
w.Write(jsn)
2014-07-10 19:19:39 +07:00
}
2014-09-24 13:49:27 +04:00
func (s *Server) pubHandler(w http.ResponseWriter, r *http.Request) {
2014-07-10 19:19:39 +07:00
msg, _ := ioutil.ReadAll(r.Body)
if len(msg) == 0 {
2014-09-11 14:27:55 +04:00
msg = []byte(r.FormValue("msg"))
2014-07-10 19:19:39 +07:00
}
2014-09-11 14:27:55 +04:00
queue := r.FormValue("queue")
2014-07-10 19:19:39 +07:00
2014-09-24 13:49:27 +04:00
if ok := s.hub.Pub(queue, msg); ok {
2014-07-17 00:47:08 +07:00
w.Write([]byte("OK"))
} else {
http.Error(w, "FAIL", 500)
}
2014-07-10 19:19:39 +07:00
}
2014-09-24 13:49:27 +04:00
func (s *Server) subHandler(w http.ResponseWriter, r *http.Request) {
2014-09-11 14:27:55 +04:00
queues := strings.Split(r.FormValue("queues"), ",")
2014-09-24 16:26:05 +04:00
sub := hub.NewSubscription(queues)
2014-07-10 19:19:39 +07:00
2014-09-11 14:27:55 +04:00
finished := make(chan struct{})
2014-09-11 23:00:19 +04:00
defer close(finished)
disconnected := w.(http.CloseNotifier).CloseNotify()
2014-07-10 19:19:39 +07:00
go func() {
select {
case <-disconnected:
case <-finished:
}
2014-09-24 16:26:05 +04:00
sub.Close()
2014-07-10 19:19:39 +07:00
}()
2014-09-24 13:49:27 +04:00
go s.hub.Sub(sub)
2014-07-10 19:19:39 +07:00
2014-09-24 16:26:05 +04:00
if res, ok := <-sub.Result(); ok {
w.Header().Set("Queue", res.Queue)
w.Write(res.Message)
}
2014-07-10 19:19:39 +07:00
}
2014-09-24 19:37:33 +04: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 17:23:59 +04:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2014-09-24 19:37:33 +04:00
w.Write(jsn)
}
2015-01-25 17:24:07 +07:00
func (s *Server) dashboardHandler(w http.ResponseWriter, r *http.Request) {
2015-01-26 15:07:23 +07:00
tmpl := template.New("dashboard")
tmpl, _ = tmpl.Parse(dashboardTmpl)
2015-01-25 17:24:07 +07: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,
})
}