From 434fe5f43e79ef01348d2814127751ef3ec490d0 Mon Sep 17 00:00:00 2001 From: conventi Date: Fri, 23 Sep 2016 00:41:42 +0200 Subject: [PATCH] Added logs info on WebSocket API --- app/main.go | 6 +++--- cli/exec.go | 9 ++++++++- cli/main.go | 26 +++++++++++++------------- cli/watcher.go | 6 +++--- server/main.go | 4 +--- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/app/main.go b/app/main.go index 56b33e6..fde2abe 100644 --- a/app/main.go +++ b/app/main.go @@ -46,7 +46,7 @@ type realize struct { Blueprint c.Blueprint Server s.Server Files map[string]string - Sync chan string + Sync chan string } // Application initialization @@ -64,12 +64,12 @@ func init() { } r.Blueprint = c.Blueprint{ Files: r.Files, - Sync: r.Sync, + Sync: r.Sync, } r.Server = s.Server{ Blueprint: &r.Blueprint, Files: r.Files, - Sync: r.Sync, + Sync: r.Sync, } r.Increase() R = &r diff --git a/cli/exec.go b/cli/exec.go index 2d253b9..b761bd6 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -16,6 +16,10 @@ import ( // GoRun is an implementation of the bin execution func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { + sync := func() { + p.parent.Sync <- "sync" + } + var build *exec.Cmd if len(p.Params) != 0 { build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path)), p.Params...) @@ -25,9 +29,12 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) build.Dir = p.base defer func() { if err := build.Process.Kill(); err != nil { + p.Buffer.StdLog = append(p.Buffer.StdLog, "Failed to stop: "+err.Error()) log.Fatal(Red("Failed to stop: "), Red(err)) } + p.Buffer.StdLog = append(p.Buffer.StdLog, "Ended") log.Println(pname(p.Name, 2), ":", RedS("Ended")) + go sync() wr.Done() }() @@ -56,7 +63,7 @@ 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" }() + go 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 ddc0c77..44ebb34 100644 --- a/cli/main.go +++ b/cli/main.go @@ -31,19 +31,19 @@ type Blueprint struct { // Project defines the informations of a single project type Project struct { - reload time.Time - base string - Name string `yaml:"app_name,omitempty"` - Path string `yaml:"app_path,omitempty"` - Run bool `yaml:"app_run,omitempty"` - Bin bool `yaml:"app_bin,omitempty"` - Build bool `yaml:"app_build,omitempty"` - Fmt bool `yaml:"app_fmt,omitempty"` - Test bool `yaml:"app_test,omitempty"` - Params []string `yaml:"app_params,omitempty"` - Watcher Watcher `yaml:"app_watcher,omitempty"` - Buffer Buffer `yaml:"-"` - parent *Blueprint + LastChangedOn time.Time + base string + Name string `yaml:"app_name,omitempty"` + Path string `yaml:"app_path,omitempty"` + Run bool `yaml:"app_run,omitempty"` + Bin bool `yaml:"app_bin,omitempty"` + Build bool `yaml:"app_build,omitempty"` + Fmt bool `yaml:"app_fmt,omitempty"` + Test bool `yaml:"app_test,omitempty"` + Params []string `yaml:"app_params,omitempty"` + Watcher Watcher `yaml:"app_watcher,omitempty"` + Buffer Buffer `yaml:"-"` + parent *Blueprint } // Watcher struct defines the livereload's logic diff --git a/cli/watcher.go b/cli/watcher.go index 051fdad..96c0db8 100644 --- a/cli/watcher.go +++ b/cli/watcher.go @@ -43,13 +43,13 @@ func (p *Project) watching() { } go p.routines(channel, &wr) - p.reload = time.Now().Truncate(time.Second) + p.LastChangedOn = time.Now().Truncate(time.Second) // waiting for an event for { select { case event := <-watcher.Events: - if time.Now().Truncate(time.Second).After(p.reload) { + if time.Now().Truncate(time.Second).After(p.LastChangedOn) { if event.Op&fsnotify.Chmod == fsnotify.Chmod { continue } @@ -76,7 +76,7 @@ func (p *Project) watching() { log.Fatal(Red(err)) } else { go p.routines(channel, &wr) - p.reload = time.Now().Truncate(time.Second) + p.LastChangedOn = time.Now().Truncate(time.Second) } } } diff --git a/server/main.go b/server/main.go index 777e02d..215271c 100644 --- a/server/main.go +++ b/server/main.go @@ -35,11 +35,9 @@ func (s *Server) Start() { e := echo.New() e.Use(middleware.Gzip()) e.GET("/", func(c echo.Context) error { - return c.JSON(200, s.Blueprint.Projects) //return render(c, "server/assets/index.html") }) - - e.GET("/projects", standard.WrapHandler(s.projects())) + e.GET("/", standard.WrapHandler(s.projects())) go e.Run(standard.New(":5000")) }