diff --git a/realize.go b/realize.go index c3e4eee..50fe112 100644 --- a/realize.go +++ b/realize.go @@ -16,6 +16,7 @@ var r realize.Realize // Realize cli commands func main() { + r.Sync = make(chan string) app := &cli.App{ Name: strings.Title(realize.RPrefix), Version: realize.RVersion, @@ -1115,6 +1116,18 @@ func setup(c *cli.Context) (err error) { // Start realize workflow func start(c *cli.Context) (err error) { r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host} + // config and start server + if c.Bool("server") || r.Server.Status { + r.Server.Status = true + if c.Bool("open") || r.Server.Open { + r.Server.Open = true + r.Server.OpenURL() + } + err = r.Server.Start() + if err != nil { + return err + } + } // check no-config and read if !c.Bool("no-config") { // read a config if exist @@ -1148,18 +1161,6 @@ func start(c *cli.Context) (err error) { } } } - // config and start server - if c.Bool("server") || r.Server.Status { - r.Server.Status = true - if c.Bool("open") || r.Server.Open { - r.Server.Open = true - r.Server.OpenURL() - } - err = r.Server.Start() - if err != nil { - return err - } - } // start workflow return r.Start() } diff --git a/realize/cli.go b/realize/cli.go index 1bffaca..45dc53a 100644 --- a/realize/cli.go +++ b/realize/cli.go @@ -35,14 +35,14 @@ type ( Realize struct { Settings Settings `yaml:"settings" json:"settings"` Server Server `yaml:"server" json:"server"` - Schema `yaml:",inline"` - sync chan string + Schema `yaml:",inline" json:",inline"` + Sync chan string `yaml:"-" json:"-"` exit chan os.Signal - Err Func `yaml:"-"` - After Func `yaml:"-"` - Before Func `yaml:"-"` - Change Func `yaml:"-"` - Reload Func `yaml:"-"` + Err Func `yaml:"-" json:"-"` + After Func `yaml:"-" json:"-"` + Before Func `yaml:"-" json:"-"` + Change Func `yaml:"-" json:"-"` + Reload Func `yaml:"-" json:"-"` } // Context is used as argument for func diff --git a/realize/projects.go b/realize/projects.go index e9a5368..a2c2064 100644 --- a/realize/projects.go +++ b/realize/projects.go @@ -529,6 +529,9 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { if stream != "" { fmt.Fprint(Output, stream) } + go func() { + p.parent.Sync <- "sync" + }() } // Run a project diff --git a/realize/server.go b/realize/server.go index 383fabc..9076456 100644 --- a/realize/server.go +++ b/realize/server.go @@ -2,10 +2,13 @@ package realize import ( "bytes" + "encoding/json" "fmt" "github.com/labstack/echo" "github.com/labstack/echo/middleware" + "golang.org/x/net/websocket" "io" + "log" "net/http" "os/exec" "runtime" @@ -15,12 +18,12 @@ import ( // Dafault host and port const ( Host = "localhost" - Port = 5001 + Port = 5002 ) // Server settings type Server struct { - Parent *Realize `yaml:"-"` + Parent *Realize `yaml:"-" json:"-"` Status bool `yaml:"status" json:"status"` Open bool `yaml:"open" json:"open"` Port int `yaml:"port" json:"port"` @@ -29,37 +32,37 @@ type Server struct { // Websocket projects func (s *Server) projects(c echo.Context) (err error) { - //websocket.Handler(func(ws *websocket.Conn) { - // msg, _ := json.Marshal(s.parent) - // err = websocket.Message.Send(ws, string(msg)) - // go func() { - // for { - // select { - // case <-s.parent.sync: - // msg, _ := json.Marshal(s.parent) - // err = websocket.Message.Send(ws, string(msg)) - // if err != nil { - // break - // } - // } - // } - // }() - // for { - // // Read - // text := "" - // err = websocket.Message.Receive(ws, &text) - // if err != nil { - // break - // } else { - // err := json.Unmarshal([]byte(text), &s.parent) - // if err == nil { - // s.parent.Settings.record(s.parent) - // break - // } - // } - // } - // ws.Close() - //}).ServeHTTP(c.Response(), c.Request()) + websocket.Handler(func(ws *websocket.Conn) { + msg, _ := json.Marshal(s.Parent) + err = websocket.Message.Send(ws, string(msg)) + go func() { + for { + select { + case <-s.Parent.Sync: + msg, _ := json.Marshal(s.Parent) + err = websocket.Message.Send(ws, string(msg)) + if err != nil { + break + } + } + } + }() + for { + // Read + text := "" + err = websocket.Message.Receive(ws, &text) + if err != nil { + break + } else { + err := json.Unmarshal([]byte(text), &s.Parent) + if err == nil { + s.Parent.Settings.Write(s.Parent) + break + } + } + } + ws.Close() + }).ServeHTTP(c.Response(), c.Request()) return nil } @@ -153,6 +156,7 @@ func (s *Server) Start() (err error) { e.HideBanner = true e.Debug = false go func() { + log.Println(s.Parent.Prefix("Started on " + string(s.Host) + ":" + strconv.Itoa(s.Port))) e.Start(string(s.Host) + ":" + strconv.Itoa(s.Port)) }() return nil