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 Blueprint c.Blueprint
Server s.Server Server s.Server
Files map[string]string Files map[string]string
Sync chan string
} }
// Application initialization // Application initialization
@ -59,11 +60,16 @@ func init() {
"config": Config, "config": Config,
"output": Output, "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{ r.Server = s.Server{
Blueprint: &r.Blueprint, Blueprint: &r.Blueprint,
Files: r.Files, Files: r.Files,
Sync: r.Sync,
} }
r.Increase() r.Increase()
R = &r R = &r

View File

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

View File

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

View File

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