sync fixed

This commit is contained in:
asoseil 2017-12-04 22:51:23 +01:00
parent a8c4aadef3
commit 311a467f06
4 changed files with 60 additions and 52 deletions

View File

@ -16,6 +16,7 @@ var r realize.Realize
// Realize cli commands // Realize cli commands
func main() { func main() {
r.Sync = make(chan string)
app := &cli.App{ app := &cli.App{
Name: strings.Title(realize.RPrefix), Name: strings.Title(realize.RPrefix),
Version: realize.RVersion, Version: realize.RVersion,
@ -1115,6 +1116,18 @@ func setup(c *cli.Context) (err error) {
// Start realize workflow // Start realize workflow
func start(c *cli.Context) (err error) { func start(c *cli.Context) (err error) {
r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host} 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 // check no-config and read
if !c.Bool("no-config") { if !c.Bool("no-config") {
// read a config if exist // 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 // start workflow
return r.Start() return r.Start()
} }

View File

@ -35,14 +35,14 @@ type (
Realize struct { Realize struct {
Settings Settings `yaml:"settings" json:"settings"` Settings Settings `yaml:"settings" json:"settings"`
Server Server `yaml:"server" json:"server"` Server Server `yaml:"server" json:"server"`
Schema `yaml:",inline"` Schema `yaml:",inline" json:",inline"`
sync chan string Sync chan string `yaml:"-" json:"-"`
exit chan os.Signal exit chan os.Signal
Err Func `yaml:"-"` Err Func `yaml:"-" json:"-"`
After Func `yaml:"-"` After Func `yaml:"-" json:"-"`
Before Func `yaml:"-"` Before Func `yaml:"-" json:"-"`
Change Func `yaml:"-"` Change Func `yaml:"-" json:"-"`
Reload Func `yaml:"-"` Reload Func `yaml:"-" json:"-"`
} }
// Context is used as argument for func // Context is used as argument for func

View File

@ -529,6 +529,9 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
if stream != "" { if stream != "" {
fmt.Fprint(Output, stream) fmt.Fprint(Output, stream)
} }
go func() {
p.parent.Sync <- "sync"
}()
} }
// Run a project // Run a project

View File

@ -2,10 +2,13 @@ package realize
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/middleware" "github.com/labstack/echo/middleware"
"golang.org/x/net/websocket"
"io" "io"
"log"
"net/http" "net/http"
"os/exec" "os/exec"
"runtime" "runtime"
@ -15,12 +18,12 @@ import (
// Dafault host and port // Dafault host and port
const ( const (
Host = "localhost" Host = "localhost"
Port = 5001 Port = 5002
) )
// Server settings // Server settings
type Server struct { type Server struct {
Parent *Realize `yaml:"-"` Parent *Realize `yaml:"-" json:"-"`
Status bool `yaml:"status" json:"status"` Status bool `yaml:"status" json:"status"`
Open bool `yaml:"open" json:"open"` Open bool `yaml:"open" json:"open"`
Port int `yaml:"port" json:"port"` Port int `yaml:"port" json:"port"`
@ -29,37 +32,37 @@ type Server struct {
// Websocket projects // Websocket projects
func (s *Server) projects(c echo.Context) (err error) { func (s *Server) projects(c echo.Context) (err error) {
//websocket.Handler(func(ws *websocket.Conn) { websocket.Handler(func(ws *websocket.Conn) {
// msg, _ := json.Marshal(s.parent) msg, _ := json.Marshal(s.Parent)
// err = websocket.Message.Send(ws, string(msg)) err = websocket.Message.Send(ws, string(msg))
// go func() { go func() {
// for { for {
// select { select {
// case <-s.parent.sync: case <-s.Parent.Sync:
// msg, _ := json.Marshal(s.parent) msg, _ := json.Marshal(s.Parent)
// err = websocket.Message.Send(ws, string(msg)) err = websocket.Message.Send(ws, string(msg))
// if err != nil { if err != nil {
// break break
// } }
// } }
// } }
// }() }()
// for { for {
// // Read // Read
// text := "" text := ""
// err = websocket.Message.Receive(ws, &text) err = websocket.Message.Receive(ws, &text)
// if err != nil { if err != nil {
// break break
// } else { } else {
// err := json.Unmarshal([]byte(text), &s.parent) err := json.Unmarshal([]byte(text), &s.Parent)
// if err == nil { if err == nil {
// s.parent.Settings.record(s.parent) s.Parent.Settings.Write(s.Parent)
// break break
// } }
// } }
// } }
// ws.Close() ws.Close()
//}).ServeHTTP(c.Response(), c.Request()) }).ServeHTTP(c.Response(), c.Request())
return nil return nil
} }
@ -153,6 +156,7 @@ func (s *Server) Start() (err error) {
e.HideBanner = true e.HideBanner = true
e.Debug = false e.Debug = false
go func() { 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)) e.Start(string(s.Host) + ":" + strconv.Itoa(s.Port))
}() }()
return nil return nil