From ce7395cfe7f85a1db9f372a23828d62f9ad94410 Mon Sep 17 00:00:00 2001 From: Cody Soyland Date: Thu, 17 Oct 2013 16:55:13 -0500 Subject: [PATCH] Basic http support. --- commands/pilosa-cruncher/cruncher.go | 16 ++++++---- commands/pilosa-router/router.go | 16 ++++++---- core/service.go | 44 ++++++++++++++-------------- cruncher/cruncher.go | 5 ++-- router/router.go | 4 +-- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/commands/pilosa-cruncher/cruncher.go b/commands/pilosa-cruncher/cruncher.go index 0336de333..fe714a458 100644 --- a/commands/pilosa-cruncher/cruncher.go +++ b/commands/pilosa-cruncher/cruncher.go @@ -7,19 +7,25 @@ import ( "log" ) -var locationString string +var tcpLoc string +var httpLoc string func init() { - flag.StringVar(&locationString, "l", "127.0.0.1:1300", "ip:port to listen on") + flag.StringVar(&tcpLoc, "l", "127.0.0.1:1300", "ip:port to listen on (tcp)") + flag.StringVar(&httpLoc, "h", "127.0.0.1:1400", "ip:port to listen on (http)") flag.Parse() } func main() { - location, err := core.NewLocation(locationString) + tcp, err := core.NewLocation(tcpLoc) if err != nil { - log.Fatal("Location not valid:", locationString) + log.Fatal("Location not valid:", tcpLoc) + } + http, err := core.NewLocation(httpLoc) + if err != nil { + log.Fatal("Location not valid:", httpLoc) } - cruncher := cruncher.NewCruncher(location) + cruncher := cruncher.NewCruncher(tcp, http) cruncher.Run() } diff --git a/commands/pilosa-router/router.go b/commands/pilosa-router/router.go index e56e7cde6..b0e132e9c 100644 --- a/commands/pilosa-router/router.go +++ b/commands/pilosa-router/router.go @@ -7,19 +7,25 @@ import ( "log" ) -var locationString string +var tcpLoc string +var httpLoc string func init() { - flag.StringVar(&locationString, "l", "127.0.0.1:1200", "ip:port to listen on") + flag.StringVar(&tcpLoc, "l", "127.0.0.1:1200", "ip:port to listen on") + flag.StringVar(&httpLoc, "h", "127.0.0.1:1400", "ip:port to listen on (http)") flag.Parse() } func main() { - location, err := core.NewLocation(locationString) + tcp, err := core.NewLocation(tcpLoc) if err != nil { - log.Fatal("Location not valid:", locationString) + log.Fatal("Location not valid:", tcpLoc) + } + http, err := core.NewLocation(httpLoc) + if err != nil { + log.Fatal("Location not valid:", httpLoc) } - router := router.NewRouter(location) + router := router.NewRouter(tcp, http) router.Run() } diff --git a/core/service.go b/core/service.go index b10cdbc8a..ced30d9b4 100644 --- a/core/service.go +++ b/core/service.go @@ -15,8 +15,8 @@ import ( //"net" //"flag" //"encoding/gob" - //"net/http" - //"encoding/json" + "net/http" + "encoding/json" //"io" ) type Message struct { @@ -77,6 +77,7 @@ type Service struct { PortHttp string Etcd *etcd.Client Location *Location + HttpLocation *Location NodeMap NodeMap NodeMapMutex sync.RWMutex Outbox chan *Envelope @@ -87,9 +88,10 @@ type Service struct { //Cluster query.Cluster } -func NewService(location *Location) *Service { +func NewService(tcp, http *Location) *Service { service := new(Service) - service.Location = location + service.Location = tcp + service.HttpLocation = http service.Outbox = make(chan *Envelope) service.Inbox = make(chan *Message) return service @@ -393,21 +395,19 @@ func (service *Service) Serve() { } } -//func (app *Application) serveHTTP() { -// http.HandleFunc("/message", func(w http.ResponseWriter, r *http.Request) { -// if r.Method != "POST" { -// http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed) -// return -// } -// var message Message -// decoder := json.NewDecoder(r.Body) -// if decoder.Decode(&message) != nil { -// http.Error(w, "Invalid JSON", http.StatusBadRequest) -// return -// } -// app.Mailbox <- message -// }) -// http.ListenAndServe(app.PortHttp, nil) -//} -// - +func (service *Service) ServeHTTP() { + http.HandleFunc("/message", func(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed) + return + } + var message Message + decoder := json.NewDecoder(r.Body) + if decoder.Decode(&message) != nil { + http.Error(w, "Invalid JSON", http.StatusBadRequest) + return + } + service.Inbox <- &message + }) + http.ListenAndServe(string(service.HttpLocation.Port), nil) +} diff --git a/cruncher/cruncher.go b/cruncher/cruncher.go index 3217f0388..31730c6bc 100644 --- a/cruncher/cruncher.go +++ b/cruncher/cruncher.go @@ -22,6 +22,7 @@ func (c *Cruncher) Run() { c.SetupNetwork() go c.Serve() go c.HandleInbox() + go c.ServeHTTP() sigterm, sighup := c.GetSignals() for { @@ -37,8 +38,8 @@ func (c *Cruncher) Run() { } } -func NewCruncher(location *core.Location) *Cruncher { - service := core.NewService(location) +func NewCruncher(tcp, http *core.Location) *Cruncher { + service := core.NewService(tcp, http) cruncher := Cruncher{*service} return &cruncher } diff --git a/router/router.go b/router/router.go index dc0dcaf7c..e576998b4 100644 --- a/router/router.go +++ b/router/router.go @@ -75,8 +75,8 @@ func (r *Router) HandleMessage(m *core.Message) { log.Println(m) } -func NewRouter(location *core.Location) *Router { - service := core.NewService(location) +func NewRouter(tcp, http *core.Location) *Router { + service := core.NewService(tcp, http) router := Router{*service} return &router }