From cdb69077706ef0b28071dc92cf3128c5718145e5 Mon Sep 17 00:00:00 2001 From: conventi Date: Sat, 17 Sep 2016 14:38:23 +0200 Subject: [PATCH] Initialization server part for WebSocket --- cli/cmd.go | 24 ------------------------ cli/exec.go | 2 +- cli/main.go | 12 ++---------- cli/utils.go | 11 ++++++++++- cli/watcher.go | 2 +- realize.go | 45 ++++++++++++++++++++++++++++++++++----------- server/main.go | 17 +++++++++++++++-- 7 files changed, 63 insertions(+), 50 deletions(-) diff --git a/cli/cmd.go b/cli/cmd.go index 4e40409..3329caa 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -5,34 +5,10 @@ import ( "fmt" "gopkg.in/urfave/cli.v2" "gopkg.in/yaml.v2" - "log" - "os" "path/filepath" "strings" - "syscall" ) -// Wdir returns the name last element of the working directory path -func (r *Realize) Wdir() string { - dir, err := os.Getwd() - if err != nil { - log.Fatal(Red(err)) - } - return filepath.Base(dir) -} - -// Flimit defines the max number of watched files -func (r *Realize) Increases() { - // increases the files limit - var rLimit syscall.Rlimit - rLimit.Max = r.Limit - rLimit.Cur = r.Limit - err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) - if err != nil { - fmt.Println(Red("Error Setting Rlimit "), err) - } -} - // Watch method adds the given paths on the Watcher func (h *Blueprint) Run() error { err := h.Read() diff --git a/cli/exec.go b/cli/exec.go index 29cbfa8..405af80 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -56,7 +56,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) log.Println(pname(p.Name, 3), ":", BlueS(in.Text())) } if p.Watcher.Output["file"] { - path := filepath.Join(p.base, App.Blueprint.Files["output"]) + path := filepath.Join(p.base, Bp.Files["output"]) f := create(path) t := time.Now() if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + in.Text() + "\r\n"); err != nil { diff --git a/cli/main.go b/cli/main.go index 1239fe5..196c19f 100644 --- a/cli/main.go +++ b/cli/main.go @@ -7,8 +7,6 @@ import ( "time" ) -var App Realize - var wg sync.WaitGroup // Green, Red Bold, Red, Blue, Blue Bold, Yellow, Yellow Bold, Magenta, Magenta Bold colors @@ -22,14 +20,6 @@ var Green, Red, RedS, Blue, BlueS, Yellow, YellowS, Magenta, MagentaS = color.Ne color.New(color.FgMagenta, color.Bold).SprintFunc(), color.New(color.FgMagenta).SprintFunc() -// Realize struct contains the general app informations -type Realize struct { - Name, Description, Author, Email string - Version string - Limit uint64 - Blueprint Blueprint -} - // Projects struct contains a projects list type Blueprint struct { Projects []Project `yaml:"Projects,omitempty"` @@ -68,3 +58,5 @@ func init() { log.SetFlags(0) log.SetOutput(new(logWriter)) } + +var Bp Blueprint diff --git a/cli/utils.go b/cli/utils.go index 4c7fb3b..ef3601a 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -12,6 +12,15 @@ import ( "time" ) +// Wdir returns the name last element of the working directory path +func Wdir() string { + dir, err := os.Getwd() + if err != nil { + log.Fatal(Red(err)) + } + return filepath.Base(dir) +} + // Read a file given a name and return its byte stream func read(file string) ([]byte, error) { _, err := os.Stat(file) @@ -62,7 +71,7 @@ func argsParam(params *cli.Context) []string { func nameFlag(params *cli.Context) string { var name string if params.String("name") == "" && params.String("path") == "" { - return App.Wdir() + return Wdir() } else if params.String("path") != "/" { name = filepath.Base(params.String("path")) } else { diff --git a/cli/watcher.go b/cli/watcher.go index 2e30959..334d06b 100644 --- a/cli/watcher.go +++ b/cli/watcher.go @@ -216,7 +216,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error { if p.Path == "." || p.Path == "/" { p.base = wd - p.Path = App.Wdir() + p.Path = Wdir() } else if filepath.IsAbs(p.Path) { p.base = p.Path } else { diff --git a/realize.go b/realize.go index 0df2aa6..0da49d8 100644 --- a/realize.go +++ b/realize.go @@ -1,23 +1,45 @@ package main import ( - r "github.com/tockins/realize/cli" - //_ "github.com/tockins/realize/server" "fmt" + c "github.com/tockins/realize/cli" + s "github.com/tockins/realize/server" "gopkg.in/urfave/cli.v2" "log" "os" + "syscall" ) -var App r.Realize +var App Realize + +// Realize struct contains the general app informations +type Realize struct { + Name, Description, Author, Email string + Version string + Limit uint64 + Blueprint c.Blueprint + Server s.Server +} + +// Flimit defines the max number of watched files +func (r *Realize) Increases() { + // increases the files limit + var rLimit syscall.Rlimit + rLimit.Max = r.Limit + rLimit.Cur = r.Limit + err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) + if err != nil { + fmt.Println(c.Red("Error Setting Rlimit "), err) + } +} func init() { - App = r.Realize{ + App = Realize{ Name: "Realize", Version: "1.0", Description: "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths", Limit: 10000, - Blueprint: r.Blueprint{ + Blueprint: c.Blueprint{ Files: map[string]string{ "config": "r.config.yaml", "output": "r.output.log", @@ -25,25 +47,25 @@ func init() { }, } App.Increases() - r.App = App + c.Bp = App.Blueprint } func main() { handle := func(err error) error { if err != nil { - fmt.Println(r.Red(err.Error())) + fmt.Println(c.Red(err.Error())) return nil } return nil } header := func() error { - fmt.Println(r.Blue(App.Name) + " - " + r.Blue(App.Version)) - fmt.Println(r.BlueS(App.Description) + "\n") + fmt.Println(c.Blue(App.Name) + " - " + c.Blue(App.Version)) + fmt.Println(c.BlueS(App.Description) + "\n") gopath := os.Getenv("GOPATH") if gopath == "" { - log.Fatal(r.Red("$GOPATH isn't set up properly")) + log.Fatal(c.Red("$GOPATH isn't set up properly")) } return nil } @@ -88,6 +110,7 @@ func main() { }, Action: func(p *cli.Context) error { App.Blueprint.Add(p) + App.Server.Start() return handle(App.Blueprint.Fast(p)) }, Before: func(c *cli.Context) error { @@ -101,7 +124,7 @@ func main() { Aliases: []string{"a"}, Usage: "Add another project", Flags: []cli.Flag{ - &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: App.Wdir(), Usage: "Project name"}, + &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: c.Wdir(), Usage: "Project name"}, &cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "/", Usage: "Project base path"}, &cli.BoolFlag{Name: "build", Value: false, Usage: "Enable the build"}, &cli.BoolFlag{Name: "no-run", Usage: "Disables the run"}, diff --git a/server/main.go b/server/main.go index 1ff6f8b..3b5281c 100644 --- a/server/main.go +++ b/server/main.go @@ -1,13 +1,19 @@ package server import ( + "fmt" "github.com/labstack/echo" "github.com/labstack/echo/engine/standard" "github.com/labstack/echo/middleware" + "golang.org/x/net/websocket" "net/http" ) -func render(c echo.Context, path string) error{ +// Server struct contains server informations +type Server struct { +} + +func render(c echo.Context, path string) error { data, err := Asset(path) if err != nil { return echo.NewHTTPError(http.StatusNotFound) @@ -19,11 +25,18 @@ func render(c echo.Context, path string) error{ return nil } -func init() { +func (s *Server) Start() { e := echo.New() e.Use(middleware.Gzip()) e.GET("/", func(c echo.Context) error { return render(c, "server/assets/index.html") }) + e.GET("/ws", standard.WrapHandler(projects())) go e.Run(standard.New(":5000")) } + +func projects() websocket.Handler { + return websocket.Handler(func(ws *websocket.Conn) { + fmt.Println(12) + }) +}