server moved in a single file
This commit is contained in:
parent
5f1ca705e1
commit
6ec3780182
103
realize.go
103
realize.go
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"github.com/tockins/interact"
|
||||
"github.com/tockins/realize/server"
|
||||
"github.com/tockins/realize/settings"
|
||||
"github.com/tockins/realize/style"
|
||||
"github.com/tockins/realize/watcher"
|
||||
|
@ -23,9 +22,9 @@ const (
|
|||
type realize struct {
|
||||
settings.Settings `yaml:"settings,omitempty"`
|
||||
Sync chan string `yaml:"-"`
|
||||
Blueprint watcher.Blueprint `yaml:"-"`
|
||||
Server server.Server `yaml:"-"`
|
||||
Projects *[]watcher.Project `yaml:"projects" json:"projects"`
|
||||
blueprint watcher.Blueprint `yaml:"-"`
|
||||
server server `yaml:"-"`
|
||||
projects *[]watcher.Project `yaml:"projects" json:"projects"`
|
||||
}
|
||||
|
||||
// New realize instance
|
||||
|
@ -67,7 +66,7 @@ func main() {
|
|||
},
|
||||
Action: func(p *cli.Context) error {
|
||||
polling(p, &r.Legacy)
|
||||
if err := insert(p, &r.Blueprint); err != nil {
|
||||
if err := insert(p, &r.blueprint); err != nil {
|
||||
return err
|
||||
}
|
||||
if !p.Bool("no-config") {
|
||||
|
@ -75,10 +74,10 @@ func main() {
|
|||
return err
|
||||
}
|
||||
}
|
||||
if err := r.Server.Start(p); err != nil {
|
||||
if err := r.server.start(p); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.Blueprint.Run(p); err != nil {
|
||||
if err := r.blueprint.Run(p); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -101,7 +100,7 @@ func main() {
|
|||
&cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: false, Usage: "Enable go run"},
|
||||
},
|
||||
Action: func(p *cli.Context) error {
|
||||
if err := r.Blueprint.Add(p); err != nil {
|
||||
if err := r.blueprint.Add(p); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.Record(r); err != nil {
|
||||
|
@ -147,11 +146,11 @@ func main() {
|
|||
Server: settings.Server{
|
||||
Status: false,
|
||||
Open: false,
|
||||
Host: server.Host,
|
||||
Port: server.Port,
|
||||
Host: host,
|
||||
Port: port,
|
||||
},
|
||||
}
|
||||
r.Blueprint.Projects = r.Blueprint.Projects[len(r.Blueprint.Projects):]
|
||||
r.blueprint.Projects = r.blueprint.Projects[len(r.blueprint.Projects):]
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
@ -261,7 +260,7 @@ func main() {
|
|||
Subs: []*interact.Question{
|
||||
{
|
||||
Before: func(d interact.Context) error {
|
||||
d.SetDef(server.Port, style.Green.Regular("("+strconv.Itoa(server.Port)+")"))
|
||||
d.SetDef(port, style.Green.Regular("("+strconv.Itoa(port)+")"))
|
||||
return nil
|
||||
},
|
||||
Quest: interact.Quest{
|
||||
|
@ -279,7 +278,7 @@ func main() {
|
|||
},
|
||||
{
|
||||
Before: func(d interact.Context) error {
|
||||
d.SetDef(server.Host, style.Green.Regular("("+server.Host+")"))
|
||||
d.SetDef(host, style.Green.Regular("("+host+")"))
|
||||
return nil
|
||||
},
|
||||
Quest: interact.Quest{
|
||||
|
@ -344,7 +343,7 @@ func main() {
|
|||
Resolve: func(d interact.Context) bool {
|
||||
val, _ := d.Ans().Bool()
|
||||
if val {
|
||||
r.Blueprint.Add(p)
|
||||
r.blueprint.Add(p)
|
||||
}
|
||||
return val
|
||||
},
|
||||
|
@ -364,7 +363,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Name = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Name = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -383,7 +382,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Path = r.Settings.Path(val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Path = r.Settings.Path(val)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -402,7 +401,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Vet = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Vet = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -435,7 +434,7 @@ func main() {
|
|||
return d.Err()
|
||||
}
|
||||
if val != "" {
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Fmt.Args = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Fmt.Args, val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Fmt.Args = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Fmt.Args, val)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
@ -446,7 +445,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Fmt.Status = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Fmt.Status = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -479,7 +478,7 @@ func main() {
|
|||
return d.Err()
|
||||
}
|
||||
if val != "" {
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Test.Args = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Test.Args, val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Test.Args = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Test.Args, val)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
@ -490,7 +489,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Test.Status = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Test.Status = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -523,7 +522,7 @@ func main() {
|
|||
return d.Err()
|
||||
}
|
||||
if val != "" {
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Generate.Args = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Generate.Args, val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Generate.Args = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Generate.Args, val)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
@ -534,7 +533,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Generate.Status = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Generate.Status = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -567,7 +566,7 @@ func main() {
|
|||
return d.Err()
|
||||
}
|
||||
if val != "" {
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Install.Args = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Install.Args, val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Install.Args = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Install.Args, val)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
@ -578,7 +577,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Install.Status = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Install.Status = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -611,7 +610,7 @@ func main() {
|
|||
return d.Err()
|
||||
}
|
||||
if val != "" {
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Build.Args = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Build.Args, val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Build.Args = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Build.Args, val)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
@ -622,7 +621,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Build.Status = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Build.Status = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -640,7 +639,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Cmds.Run = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Cmds.Run = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -655,7 +654,7 @@ func main() {
|
|||
Resolve: func(d interact.Context) bool {
|
||||
val, _ := d.Ans().Bool()
|
||||
if val {
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths = r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths[:len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths)-1]
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Paths = r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Paths[:len(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Paths)-1]
|
||||
}
|
||||
return val
|
||||
},
|
||||
|
@ -675,7 +674,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Paths, val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Paths = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Paths, val)
|
||||
d.Reload()
|
||||
return nil
|
||||
},
|
||||
|
@ -700,7 +699,7 @@ func main() {
|
|||
Resolve: func(d interact.Context) bool {
|
||||
val, _ := d.Ans().Bool()
|
||||
if val {
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore = r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore[:len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore)-1]
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Ignore = r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Ignore[:len(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Ignore)-1]
|
||||
}
|
||||
return val
|
||||
},
|
||||
|
@ -720,7 +719,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Ignore, val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Ignore = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Ignore, val)
|
||||
d.Reload()
|
||||
return nil
|
||||
},
|
||||
|
@ -762,7 +761,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Args = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Args, val)
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Args = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Args, val)
|
||||
d.Reload()
|
||||
return nil
|
||||
},
|
||||
|
@ -804,7 +803,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val})
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "before", Command: val})
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -822,7 +821,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts[len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts)-1].Path = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts[len(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts)-1].Path = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -840,7 +839,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts[len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts)-1].Global = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts[len(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts)-1].Global = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -858,7 +857,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts[len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts)-1].Output = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts[len(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts)-1].Output = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -902,7 +901,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts = append(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val})
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts = append(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts, watcher.Command{Type: "after", Command: val})
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -920,7 +919,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts[len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts)-1].Path = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts[len(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts)-1].Path = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -938,7 +937,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts[len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts)-1].Global = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts[len(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts)-1].Global = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -956,7 +955,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts[len(r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Scripts)-1].Output = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts[len(r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Scripts)-1].Output = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -986,7 +985,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].Watcher.Preview = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].Watcher.Preview = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -1004,7 +1003,7 @@ func main() {
|
|||
if err != nil {
|
||||
return d.Err()
|
||||
}
|
||||
r.Blueprint.Projects[len(r.Blueprint.Projects)-1].ErrorOutputPattern = val
|
||||
r.blueprint.Projects[len(r.blueprint.Projects)-1].ErrorOutputPattern = val
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -1046,7 +1045,7 @@ func main() {
|
|||
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: ""},
|
||||
},
|
||||
Action: func(p *cli.Context) error {
|
||||
if err := r.Blueprint.Remove(p); err != nil {
|
||||
if err := r.blueprint.Remove(p); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.Record(r); err != nil {
|
||||
|
@ -1063,7 +1062,7 @@ func main() {
|
|||
Aliases: []string{"l"},
|
||||
Description: "Print projects list.",
|
||||
Action: func(p *cli.Context) error {
|
||||
return r.Blueprint.List()
|
||||
return r.blueprint.List()
|
||||
},
|
||||
Before: before,
|
||||
},
|
||||
|
@ -1111,21 +1110,21 @@ func before(*cli.Context) error {
|
|||
Server: settings.Server{
|
||||
Status: false,
|
||||
Open: false,
|
||||
Host: server.Host,
|
||||
Port: server.Port,
|
||||
Host: host,
|
||||
Port: port,
|
||||
},
|
||||
},
|
||||
}
|
||||
r.Blueprint = watcher.Blueprint{
|
||||
r.blueprint = watcher.Blueprint{
|
||||
Settings: &r.Settings,
|
||||
Sync: r.Sync,
|
||||
}
|
||||
r.Server = server.Server{
|
||||
Blueprint: &r.Blueprint,
|
||||
r.server = server{
|
||||
Blueprint: &r.blueprint,
|
||||
Settings: &r.Settings,
|
||||
Sync: r.Sync,
|
||||
}
|
||||
r.Projects = &r.Blueprint.Projects
|
||||
r.projects = &r.blueprint.Projects
|
||||
// read if exist
|
||||
r.Read(&r)
|
||||
// increase the file limit
|
||||
|
@ -1147,7 +1146,7 @@ func polling(c *cli.Context, s *settings.Legacy) {
|
|||
// Insert a project if there isn't already one
|
||||
func insert(c *cli.Context, b *watcher.Blueprint) error {
|
||||
if c.Bool("no-config") {
|
||||
r.Blueprint.Projects = []watcher.Project{}
|
||||
r.blueprint.Projects = []watcher.Project{}
|
||||
}
|
||||
if len(b.Projects) <= 0 {
|
||||
if err := b.Add(c); err != nil {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package server
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -11,23 +11,28 @@ import (
|
|||
"github.com/tockins/realize/watcher"
|
||||
"golang.org/x/net/websocket"
|
||||
"gopkg.in/urfave/cli.v2"
|
||||
"io"
|
||||
"runtime"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
// Dafault host and port
|
||||
const (
|
||||
Host = "localhost"
|
||||
Port = 5001
|
||||
host = "localhost"
|
||||
port = 5001
|
||||
)
|
||||
|
||||
// Server settings
|
||||
type Server struct {
|
||||
type server struct {
|
||||
*settings.Settings `yaml:"-"`
|
||||
*watcher.Blueprint `yaml:"-"`
|
||||
Sync chan string `yaml:"-"`
|
||||
}
|
||||
|
||||
// Render return a web pages defined in bindata
|
||||
func render(c echo.Context, path string, mime int) error {
|
||||
func (s *server) render(c echo.Context, path string, mime int) error {
|
||||
data, err := Asset(path)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
|
@ -57,7 +62,7 @@ func render(c echo.Context, path string, mime int) error {
|
|||
}
|
||||
|
||||
// Start the web server
|
||||
func (s *Server) Start(p *cli.Context) (err error) {
|
||||
func (s *server) start(p *cli.Context) (err error) {
|
||||
if p.Bool("server") {
|
||||
s.Server.Status = p.Bool("server")
|
||||
s.Server.Open = true
|
||||
|
@ -72,55 +77,55 @@ func (s *Server) Start(p *cli.Context) (err error) {
|
|||
|
||||
// web panel
|
||||
e.GET("/", func(c echo.Context) error {
|
||||
return render(c, "assets/index.html", 1)
|
||||
return s.render(c, "assets/index.html", 1)
|
||||
})
|
||||
e.GET("/assets/js/all.min.js", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/js/all.min.js", 2)
|
||||
return s.render(c, "assets/assets/js/all.min.js", 2)
|
||||
})
|
||||
e.GET("/assets/css/app.css", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/css/app.css", 3)
|
||||
return s.render(c, "assets/assets/css/app.css", 3)
|
||||
})
|
||||
e.GET("/app/components/settings/index.html", func(c echo.Context) error {
|
||||
return render(c, "assets/app/components/settings/index.html", 1)
|
||||
return s.render(c, "assets/app/components/settings/index.html", 1)
|
||||
})
|
||||
e.GET("/app/components/project/index.html", func(c echo.Context) error {
|
||||
return render(c, "assets/app/components/project/index.html", 1)
|
||||
return s.render(c, "assets/app/components/project/index.html", 1)
|
||||
})
|
||||
e.GET("/app/components/index.html", func(c echo.Context) error {
|
||||
return render(c, "assets/app/components/index.html", 1)
|
||||
return s.render(c, "assets/app/components/index.html", 1)
|
||||
})
|
||||
e.GET("/assets/img/svg/ic_settings_black_24px.svg", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/svg/ic_settings_black_24px.svg", 4)
|
||||
return s.render(c, "assets/assets/img/svg/ic_settings_black_24px.svg", 4)
|
||||
})
|
||||
e.GET("/assets/img/svg/ic_fullscreen_black_24px.svg", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/svg/ic_fullscreen_black_24px.svg", 4)
|
||||
return s.render(c, "assets/assets/img/svg/ic_fullscreen_black_24px.svg", 4)
|
||||
})
|
||||
e.GET("/assets/img/svg/ic_add_black_24px.svg", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/svg/ic_add_black_24px.svg", 4)
|
||||
return s.render(c, "assets/assets/img/svg/ic_add_black_24px.svg", 4)
|
||||
})
|
||||
e.GET("/assets/img/svg/ic_keyboard_backspace_black_24px.svg", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/svg/ic_keyboard_backspace_black_24px.svg", 4)
|
||||
return s.render(c, "assets/assets/img/svg/ic_keyboard_backspace_black_24px.svg", 4)
|
||||
})
|
||||
e.GET("/assets/img/svg/ic_error_black_48px.svg", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/svg/ic_error_black_48px.svg", 4)
|
||||
return s.render(c, "assets/assets/img/svg/ic_error_black_48px.svg", 4)
|
||||
})
|
||||
e.GET("/assets/img/svg/ic_remove_black_24px.svg", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/svg/ic_remove_black_24px.svg", 4)
|
||||
return s.render(c, "assets/assets/img/svg/ic_remove_black_24px.svg", 4)
|
||||
})
|
||||
e.GET("/assets/img/svg/logo.svg", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/svg/logo.svg", 4)
|
||||
return s.render(c, "assets/assets/img/svg/logo.svg", 4)
|
||||
})
|
||||
e.GET("/assets/img/favicon-32x32.png", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/favicon-32x32.png", 5)
|
||||
return s.render(c, "assets/assets/img/favicon-32x32.png", 5)
|
||||
})
|
||||
e.GET("/assets/img/svg/ic_swap_vertical_circle_black_48px.svg", func(c echo.Context) error {
|
||||
return render(c, "assets/assets/img/svg/ic_swap_vertical_circle_black_48px.svg", 4)
|
||||
return s.render(c, "assets/assets/img/svg/ic_swap_vertical_circle_black_48px.svg", 4)
|
||||
})
|
||||
|
||||
//websocket
|
||||
e.GET("/ws", s.projects)
|
||||
go e.Start(string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port))
|
||||
_, err = s.OpenURL("http://" + string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port))
|
||||
_, err = s.openURL("http://" + string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -129,7 +134,7 @@ func (s *Server) Start(p *cli.Context) (err error) {
|
|||
}
|
||||
|
||||
// Websocket projects
|
||||
func (s *Server) projects(c echo.Context) error {
|
||||
func (s *server) projects(c echo.Context) error {
|
||||
websocket.Handler(func(ws *websocket.Conn) {
|
||||
defer ws.Close()
|
||||
msg, _ := json.Marshal(s.Blueprint.Projects)
|
||||
|
@ -163,3 +168,25 @@ func (s *Server) projects(c echo.Context) error {
|
|||
}).ServeHTTP(c.Response(), c.Request())
|
||||
return nil
|
||||
}
|
||||
|
||||
// OpenURL in a new tab of default browser
|
||||
func (s *server) openURL(url string) (io.Writer, error) {
|
||||
stderr := bytes.Buffer{}
|
||||
cmd := map[string]string{
|
||||
"windows": "start",
|
||||
"darwin": "open",
|
||||
"linux": "xdg-open",
|
||||
}
|
||||
if s.Open {
|
||||
open, err := cmd[runtime.GOOS]
|
||||
if !err {
|
||||
return nil, fmt.Errorf("operating system %q is not supported", runtime.GOOS)
|
||||
}
|
||||
cmd := exec.Command(open, url)
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return cmd.Stderr, err
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
File diff suppressed because one or more lines are too long
|
@ -1,37 +0,0 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var cmd map[string]string
|
||||
var stderr bytes.Buffer
|
||||
|
||||
// Init an associative array with the os supported
|
||||
func init() {
|
||||
cmd = map[string]string{
|
||||
"windows": "start",
|
||||
"darwin": "open",
|
||||
"linux": "xdg-open",
|
||||
}
|
||||
}
|
||||
|
||||
// OpenURL is used for open an url in the default browser
|
||||
func (s *Server) OpenURL(url string) (io.Writer, error) {
|
||||
if s.Open {
|
||||
open, err := cmd[runtime.GOOS]
|
||||
if !err {
|
||||
return nil, fmt.Errorf("operating system %q is not supported", runtime.GOOS)
|
||||
}
|
||||
cmd := exec.Command(open, url)
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return cmd.Stderr, err
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package server
|
||||
|
||||
//import (
|
||||
// "testing"
|
||||
// //"fmt"
|
||||
// "github.com/tockins/realize/settings"
|
||||
// "fmt"
|
||||
//)
|
||||
//
|
||||
//func TestOpen(t *testing.T) {
|
||||
// config := settings.Settings{
|
||||
// Server: settings.Server{
|
||||
// Open: true,
|
||||
// },
|
||||
// }
|
||||
// s := Server{
|
||||
// Settings: &config,
|
||||
// }
|
||||
// url := "open_test"
|
||||
// out, err := s.OpenURL(url)
|
||||
// if err == nil {
|
||||
// t.Fatal("Unexpected, invalid url", url, err)
|
||||
// }
|
||||
// output := fmt.Sprint(out)
|
||||
// if output == "" {
|
||||
// t.Fatal("Unexpected, invalid url", url, output)
|
||||
// }
|
||||
//}
|
|
@ -1,6 +1,7 @@
|
|||
package server
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tockins/realize/settings"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
@ -48,3 +49,23 @@ func TestServer_Start(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpen(t *testing.T) {
|
||||
config := settings.Settings{
|
||||
Server: settings.Server{
|
||||
Open: true,
|
||||
},
|
||||
}
|
||||
s := Server{
|
||||
Settings: &config,
|
||||
}
|
||||
url := "open_test"
|
||||
out, err := s.openURL(url)
|
||||
if err == nil {
|
||||
t.Fatal("Unexpected, invalid url", url, err)
|
||||
}
|
||||
output := fmt.Sprint(out)
|
||||
if output == "" {
|
||||
t.Fatal("Unexpected, invalid url", url, output)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue