Initialization server part for WebSocket
This commit is contained in:
parent
d2d442bf76
commit
cdb6907770
24
cli/cmd.go
24
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()
|
||||
|
|
|
@ -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 {
|
||||
|
|
12
cli/main.go
12
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
|
||||
|
|
11
cli/utils.go
11
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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
45
realize.go
45
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"},
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue