Basic http support.

This commit is contained in:
Cody Soyland 2013-10-17 16:55:13 -05:00
parent 3f2ff1a0e3
commit ce7395cfe7
5 changed files with 49 additions and 36 deletions

View file

@ -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()
}

View file

@ -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()
}

View file

@ -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)
}

View file

@ -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
}

View file

@ -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
}