1
0
Fork 0

Move server into subpackage

This commit is contained in:
Gregory Eremin 2014-09-24 13:49:27 +04:00
parent 384ca3dd0f
commit 2a6f063ab0
2 changed files with 44 additions and 29 deletions

29
main.go
View File

@ -9,6 +9,7 @@ import (
"syscall" "syscall"
"github.com/KosyanMedia/burlesque/hub" "github.com/KosyanMedia/burlesque/hub"
"github.com/KosyanMedia/burlesque/server"
"github.com/KosyanMedia/burlesque/storage" "github.com/KosyanMedia/burlesque/storage"
) )
@ -16,20 +17,17 @@ const (
version = "0.2.0" version = "0.2.0"
) )
var (
theHub *hub.Hub
config struct {
storage string
port int
}
)
func main() { func main() {
flag.StringVar(&config.storage, "storage", "-", "Kyoto Cabinet storage path (e.g. burlesque.kch#dfunit=8#msiz=512M)") var (
flag.IntVar(&config.port, "port", 4401, "Server HTTP port") storagePath string
port int
)
flag.StringVar(&storagePath, "storage", "-", "Kyoto Cabinet storage path (e.g. burlesque.kch#dfunit=8#msiz=512M)")
flag.IntVar(&port, "port", 4401, "Server HTTP port")
flag.Parse() flag.Parse()
store, err := storage.New(config.storage) store, err := storage.New(storagePath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -44,10 +42,11 @@ func main() {
fmt.Printf("Burlesque v%s started\n", version) fmt.Printf("Burlesque v%s started\n", version)
fmt.Printf("GOMAXPROCS is set to %d\n", runtime.GOMAXPROCS(-1)) fmt.Printf("GOMAXPROCS is set to %d\n", runtime.GOMAXPROCS(-1))
fmt.Printf("Storage path: %s\n", config.storage) fmt.Printf("Storage path: %s\n", storagePath)
fmt.Printf("Server is running at http://127.0.0.1:%d\n", config.port) fmt.Printf("Server is running at http://127.0.0.1:%d\n", port)
theHub = hub.New(store) h := hub.New(store)
s := server.New(port, h)
startServer() s.Start()
} }

View File

@ -1,4 +1,4 @@
package main package server
import ( import (
"encoding/json" "encoding/json"
@ -10,25 +10,41 @@ import (
"github.com/KosyanMedia/burlesque/hub" "github.com/KosyanMedia/burlesque/hub"
) )
func startServer() { type (
http.HandleFunc("/status", statusHandler) Server struct {
http.HandleFunc("/debug", debugHandler) port int
http.HandleFunc("/publish", pubHandler) hub *hub.Hub
http.HandleFunc("/subscribe", subHandler) }
)
port := fmt.Sprintf(":%d", config.port) 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)
return &s
}
func (s *Server) Start() {
port := fmt.Sprintf(":%d", s.port)
if err := http.ListenAndServe(port, nil); err != nil { if err := http.ListenAndServe(port, nil); err != nil {
panic(err) panic(err)
} }
} }
func statusHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) statusHandler(w http.ResponseWriter, r *http.Request) {
info := theHub.Info() info := s.hub.Info()
jsn, _ := json.Marshal(info) jsn, _ := json.Marshal(info)
w.Write(jsn) w.Write(jsn)
} }
func debugHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) debugHandler(w http.ResponseWriter, r *http.Request) {
// info := make(map[string]interface{}) // info := make(map[string]interface{})
// info["version"] = version // info["version"] = version
// info["goroutines"] = runtime.NumGoroutine() // info["goroutines"] = runtime.NumGoroutine()
@ -56,7 +72,7 @@ func debugHandler(w http.ResponseWriter, r *http.Request) {
// w.Write(jsn) // w.Write(jsn)
} }
func pubHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) pubHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
msg, _ := ioutil.ReadAll(r.Body) msg, _ := ioutil.ReadAll(r.Body)
if len(msg) == 0 { if len(msg) == 0 {
@ -64,14 +80,14 @@ func pubHandler(w http.ResponseWriter, r *http.Request) {
} }
queue := r.FormValue("queue") queue := r.FormValue("queue")
if ok := theHub.Pub(queue, msg); ok { if ok := s.hub.Pub(queue, msg); ok {
w.Write([]byte("OK")) w.Write([]byte("OK"))
} else { } else {
http.Error(w, "FAIL", 500) http.Error(w, "FAIL", 500)
} }
} }
func subHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) subHandler(w http.ResponseWriter, r *http.Request) {
result := make(chan hub.Result) result := make(chan hub.Result)
queues := strings.Split(r.FormValue("queues"), ",") queues := strings.Split(r.FormValue("queues"), ",")
@ -90,7 +106,7 @@ func subHandler(w http.ResponseWriter, r *http.Request) {
} }
}() }()
go theHub.Sub(sub) go s.hub.Sub(sub)
res := <-result res := <-result
w.Header().Set("Queue", res.Queue) w.Header().Set("Queue", res.Queue)