sync channel, sharing data between the server and cli packages

This commit is contained in:
alessio 2016-09-18 15:35:44 +02:00
parent 602b0dd082
commit 4515435bca
4 changed files with 26 additions and 12 deletions

View File

@ -46,6 +46,7 @@ type realize struct {
Blueprint c.Blueprint
Server s.Server
Files map[string]string
Sync chan string
}
// Application initialization
@ -59,11 +60,16 @@ func init() {
"config": Config,
"output": Output,
},
Sync: make(chan string),
}
r.Blueprint = c.Blueprint{
Files: r.Files,
Sync: r.Sync,
}
r.Blueprint = c.Blueprint{Files: r.Files}
r.Server = s.Server{
Blueprint: &r.Blueprint,
Files: r.Files,
Sync: r.Sync,
}
r.Increase()
R = &r

View File

@ -56,6 +56,8 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
} else {
p.Buffer.StdOut = append(p.Buffer.StdOut, output.Text())
}
go func() { p.parent.Sync <- "sync" }()
if p.Watcher.Output["cli"] {
log.Println(pname(p.Name, 3), ":", BlueS(output.Text()))
}

View File

@ -26,6 +26,7 @@ type logWriter struct{}
type Blueprint struct {
Projects []Project `yaml:"projects,omitempty"`
Files map[string]string `yaml:"-"`
Sync chan string
}
// Project defines the informations of a single project

View File

@ -16,6 +16,7 @@ import (
type Server struct {
Blueprint *c.Blueprint
Files map[string]string
Sync chan string
}
func render(c echo.Context, path string) error {
@ -35,28 +36,32 @@ func (s *Server) Start() {
e := echo.New()
e.Use(middleware.Gzip())
e.GET("/", func(c echo.Context) error {
return c.JSON(200, s.Blueprint)
return c.JSON(200, s.Blueprint.Projects)
//return render(c, "server/assets/index.html")
})
e.GET("/projects", standard.WrapHandler(projects()))
e.GET("/projects", standard.WrapHandler(s.projects()))
go e.Run(standard.New(":5000"))
}
// The WebSocket for projects list
func projects() websocket.Handler {
func (s *Server) projects() websocket.Handler {
return websocket.Handler(func(ws *websocket.Conn) {
for {
message, _ := json.Marshal("")
msg := func() {
fmt.Println("tick")
message, _ := json.Marshal(s.Blueprint.Projects)
err := websocket.Message.Send(ws, string(message))
fmt.Println("")
if err != nil {
log.Fatal(err)
}
msg := ""
err = websocket.Message.Receive(ws, &msg)
if err != nil {
log.Fatal(err)
}
msg()
for {
select {
default:
continue
case <-s.Sync:
msg()
}
}
})