From 2a6f063ab0b7bf63a2d6553434b91e8e1928b0ac Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 24 Sep 2014 13:49:27 +0400 Subject: [PATCH] Move server into subpackage --- main.go | 29 +++++++++++------------ server.go => server/server.go | 44 ++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 29 deletions(-) rename server.go => server/server.go (66%) diff --git a/main.go b/main.go index cca5b50..22e6f6f 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "syscall" "github.com/KosyanMedia/burlesque/hub" + "github.com/KosyanMedia/burlesque/server" "github.com/KosyanMedia/burlesque/storage" ) @@ -16,20 +17,17 @@ const ( version = "0.2.0" ) -var ( - theHub *hub.Hub - config struct { - storage string - port int - } -) - func main() { - flag.StringVar(&config.storage, "storage", "-", "Kyoto Cabinet storage path (e.g. burlesque.kch#dfunit=8#msiz=512M)") - flag.IntVar(&config.port, "port", 4401, "Server HTTP port") + var ( + 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() - store, err := storage.New(config.storage) + store, err := storage.New(storagePath) if err != nil { panic(err) } @@ -44,10 +42,11 @@ func main() { fmt.Printf("Burlesque v%s started\n", version) fmt.Printf("GOMAXPROCS is set to %d\n", runtime.GOMAXPROCS(-1)) - fmt.Printf("Storage path: %s\n", config.storage) - fmt.Printf("Server is running at http://127.0.0.1:%d\n", config.port) + fmt.Printf("Storage path: %s\n", storagePath) + 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() } diff --git a/server.go b/server/server.go similarity index 66% rename from server.go rename to server/server.go index d7903aa..73288f6 100644 --- a/server.go +++ b/server/server.go @@ -1,4 +1,4 @@ -package main +package server import ( "encoding/json" @@ -10,25 +10,41 @@ import ( "github.com/KosyanMedia/burlesque/hub" ) -func startServer() { - http.HandleFunc("/status", statusHandler) - http.HandleFunc("/debug", debugHandler) - http.HandleFunc("/publish", pubHandler) - http.HandleFunc("/subscribe", subHandler) +type ( + Server struct { + port int + hub *hub.Hub + } +) - 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 { panic(err) } } -func statusHandler(w http.ResponseWriter, r *http.Request) { - info := theHub.Info() +func (s *Server) statusHandler(w http.ResponseWriter, r *http.Request) { + info := s.hub.Info() jsn, _ := json.Marshal(info) 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["version"] = version // info["goroutines"] = runtime.NumGoroutine() @@ -56,7 +72,7 @@ func debugHandler(w http.ResponseWriter, r *http.Request) { // 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() msg, _ := ioutil.ReadAll(r.Body) if len(msg) == 0 { @@ -64,14 +80,14 @@ func pubHandler(w http.ResponseWriter, r *http.Request) { } queue := r.FormValue("queue") - if ok := theHub.Pub(queue, msg); ok { + if ok := s.hub.Pub(queue, msg); ok { w.Write([]byte("OK")) } else { 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) 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 w.Header().Set("Queue", res.Queue)