From 4515435bcac80f2fd96584b2889fd5bedac379ac Mon Sep 17 00:00:00 2001 From: alessio Date: Sun, 18 Sep 2016 15:35:44 +0200 Subject: [PATCH] sync channel, sharing data between the server and cli packages --- app/main.go | 8 +++++++- cli/exec.go | 2 ++ cli/main.go | 1 + server/main.go | 27 ++++++++++++++++----------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/main.go b/app/main.go index ee43d25..56b33e6 100644 --- a/app/main.go +++ b/app/main.go @@ -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 diff --git a/cli/exec.go b/cli/exec.go index 500afd6..2d253b9 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -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())) } diff --git a/cli/main.go b/cli/main.go index 01c51a7..ddc0c77 100644 --- a/cli/main.go +++ b/cli/main.go @@ -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 diff --git a/server/main.go b/server/main.go index fd04339..f96ab35 100644 --- a/server/main.go +++ b/server/main.go @@ -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,29 +36,33 @@ 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() + } + } }) }