Initialization server part for WebSocket

This commit is contained in:
conventi 2016-09-17 14:38:23 +02:00
parent d2d442bf76
commit cdb6907770
7 changed files with 63 additions and 50 deletions

View File

@ -5,34 +5,10 @@ import (
"fmt" "fmt"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"log"
"os"
"path/filepath" "path/filepath"
"strings" "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 // Watch method adds the given paths on the Watcher
func (h *Blueprint) Run() error { func (h *Blueprint) Run() error {
err := h.Read() err := h.Read()

View File

@ -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())) log.Println(pname(p.Name, 3), ":", BlueS(in.Text()))
} }
if p.Watcher.Output["file"] { 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) f := create(path)
t := time.Now() t := time.Now()
if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + in.Text() + "\r\n"); err != nil { if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + in.Text() + "\r\n"); err != nil {

View File

@ -7,8 +7,6 @@ import (
"time" "time"
) )
var App Realize
var wg sync.WaitGroup var wg sync.WaitGroup
// Green, Red Bold, Red, Blue, Blue Bold, Yellow, Yellow Bold, Magenta, Magenta Bold colors // 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, color.Bold).SprintFunc(),
color.New(color.FgMagenta).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 // Projects struct contains a projects list
type Blueprint struct { type Blueprint struct {
Projects []Project `yaml:"Projects,omitempty"` Projects []Project `yaml:"Projects,omitempty"`
@ -68,3 +58,5 @@ func init() {
log.SetFlags(0) log.SetFlags(0)
log.SetOutput(new(logWriter)) log.SetOutput(new(logWriter))
} }
var Bp Blueprint

View File

@ -12,6 +12,15 @@ import (
"time" "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 // Read a file given a name and return its byte stream
func read(file string) ([]byte, error) { func read(file string) ([]byte, error) {
_, err := os.Stat(file) _, err := os.Stat(file)
@ -62,7 +71,7 @@ func argsParam(params *cli.Context) []string {
func nameFlag(params *cli.Context) string { func nameFlag(params *cli.Context) string {
var name string var name string
if params.String("name") == "" && params.String("path") == "" { if params.String("name") == "" && params.String("path") == "" {
return App.Wdir() return Wdir()
} else if params.String("path") != "/" { } else if params.String("path") != "/" {
name = filepath.Base(params.String("path")) name = filepath.Base(params.String("path"))
} else { } else {

View File

@ -216,7 +216,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error {
if p.Path == "." || p.Path == "/" { if p.Path == "." || p.Path == "/" {
p.base = wd p.base = wd
p.Path = App.Wdir() p.Path = Wdir()
} else if filepath.IsAbs(p.Path) { } else if filepath.IsAbs(p.Path) {
p.base = p.Path p.base = p.Path
} else { } else {

View File

@ -1,23 +1,45 @@
package main package main
import ( import (
r "github.com/tockins/realize/cli"
//_ "github.com/tockins/realize/server"
"fmt" "fmt"
c "github.com/tockins/realize/cli"
s "github.com/tockins/realize/server"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"log" "log"
"os" "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() { func init() {
App = r.Realize{ App = Realize{
Name: "Realize", Name: "Realize",
Version: "1.0", 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", Description: "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths",
Limit: 10000, Limit: 10000,
Blueprint: r.Blueprint{ Blueprint: c.Blueprint{
Files: map[string]string{ Files: map[string]string{
"config": "r.config.yaml", "config": "r.config.yaml",
"output": "r.output.log", "output": "r.output.log",
@ -25,25 +47,25 @@ func init() {
}, },
} }
App.Increases() App.Increases()
r.App = App c.Bp = App.Blueprint
} }
func main() { func main() {
handle := func(err error) error { handle := func(err error) error {
if err != nil { if err != nil {
fmt.Println(r.Red(err.Error())) fmt.Println(c.Red(err.Error()))
return nil return nil
} }
return nil return nil
} }
header := func() error { header := func() error {
fmt.Println(r.Blue(App.Name) + " - " + r.Blue(App.Version)) fmt.Println(c.Blue(App.Name) + " - " + c.Blue(App.Version))
fmt.Println(r.BlueS(App.Description) + "\n") fmt.Println(c.BlueS(App.Description) + "\n")
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")
if 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 return nil
} }
@ -88,6 +110,7 @@ func main() {
}, },
Action: func(p *cli.Context) error { Action: func(p *cli.Context) error {
App.Blueprint.Add(p) App.Blueprint.Add(p)
App.Server.Start()
return handle(App.Blueprint.Fast(p)) return handle(App.Blueprint.Fast(p))
}, },
Before: func(c *cli.Context) error { Before: func(c *cli.Context) error {
@ -101,7 +124,7 @@ func main() {
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "Add another project", Usage: "Add another project",
Flags: []cli.Flag{ 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.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "/", Usage: "Project base path"},
&cli.BoolFlag{Name: "build", Value: false, Usage: "Enable the build"}, &cli.BoolFlag{Name: "build", Value: false, Usage: "Enable the build"},
&cli.BoolFlag{Name: "no-run", Usage: "Disables the run"}, &cli.BoolFlag{Name: "no-run", Usage: "Disables the run"},

View File

@ -1,13 +1,19 @@
package server package server
import ( import (
"fmt"
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/engine/standard" "github.com/labstack/echo/engine/standard"
"github.com/labstack/echo/middleware" "github.com/labstack/echo/middleware"
"golang.org/x/net/websocket"
"net/http" "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) data, err := Asset(path)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusNotFound) return echo.NewHTTPError(http.StatusNotFound)
@ -19,11 +25,18 @@ func render(c echo.Context, path string) error{
return nil return nil
} }
func init() { 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 render(c, "server/assets/index.html") return render(c, "server/assets/index.html")
}) })
e.GET("/ws", standard.WrapHandler(projects()))
go e.Run(standard.New(":5000")) go e.Run(standard.New(":5000"))
} }
func projects() websocket.Handler {
return websocket.Handler(func(ws *websocket.Conn) {
fmt.Println(12)
})
}