This commit is contained in:
Ian Kent 2014-12-21 09:18:19 +00:00
parent 309b4cde67
commit d7093a57a7
2 changed files with 11 additions and 18 deletions

View file

@ -11,18 +11,16 @@ import (
"github.com/ian-kent/go-log/log" "github.com/ian-kent/go-log/log"
gotcha "github.com/ian-kent/gotcha/app" gotcha "github.com/ian-kent/gotcha/app"
"github.com/ian-kent/gotcha/http" "github.com/ian-kent/gotcha/http"
"github.com/ian-kent/goose"
) )
type APIv1 struct { type APIv1 struct {
config *config.Config config *config.Config
eventlisteners []*EventListener app *gotcha.App
app *gotcha.App
} }
type EventListener struct { var stream *goose.EventStream
session *http.Session
ch chan []byte
}
type ReleaseConfig struct { type ReleaseConfig struct {
Email string Email string
@ -33,11 +31,11 @@ type ReleaseConfig struct {
func CreateAPIv1(conf *config.Config, app *gotcha.App) *APIv1 { func CreateAPIv1(conf *config.Config, app *gotcha.App) *APIv1 {
log.Println("Creating API v1") log.Println("Creating API v1")
apiv1 := &APIv1{ apiv1 := &APIv1{
config: conf, config: conf,
eventlisteners: make([]*EventListener, 0), app: app,
app: app,
} }
stream = goose.NewEventStream()
r := app.Router r := app.Router
r.Get("/api/v1/messages/?", apiv1.messages) r.Get("/api/v1/messages/?", apiv1.messages)
@ -68,19 +66,13 @@ func CreateAPIv1(conf *config.Config, app *gotcha.App) *APIv1 {
func (apiv1 *APIv1) broadcast(json string) { func (apiv1 *APIv1) broadcast(json string) {
log.Println("[APIv1] BROADCAST /api/v1/events") log.Println("[APIv1] BROADCAST /api/v1/events")
b := []byte(json) b := []byte(json)
for _, l := range apiv1.eventlisteners { stream.Notify("data", b)
log.Printf("Sending to connection: %s\n", l.session.Request.RemoteAddr)
l.ch <- b
}
} }
func (apiv1 *APIv1) eventstream(session *http.Session) { func (apiv1 *APIv1) eventstream(session *http.Session) {
log.Println("[APIv1] GET /api/v1/events") log.Println("[APIv1] GET /api/v1/events")
apiv1.eventlisteners = append(apiv1.eventlisteners, &EventListener{ stream.AddReceiver(session.Response.GetWriter())
session,
session.Response.EventStream(),
})
} }
func (apiv1 *APIv1) messages(session *http.Session) { func (apiv1 *APIv1) messages(session *http.Session) {

View file

@ -24,6 +24,7 @@ deps:
go get github.com/ian-kent/gotcha/gotcha go get github.com/ian-kent/gotcha/gotcha
go get github.com/ian-kent/go-log/log go get github.com/ian-kent/go-log/log
go get github.com/ian-kent/envconf go get github.com/ian-kent/envconf
go get github.com/ian-kent/goose
go get github.com/jteeuwen/go-bindata/... go get github.com/jteeuwen/go-bindata/...
go get labix.org/v2/mgo go get labix.org/v2/mgo
# added to fix travis issues # added to fix travis issues