web server init improved

This commit is contained in:
asoseil 2018-02-08 14:42:59 +01:00
parent e1ec6e46c4
commit 9146347ec9
3 changed files with 83 additions and 75 deletions

View File

@ -1117,10 +1117,13 @@ func setup(c *cli.Context) (err error) {
// Start realize workflow
func start(c *cli.Context) (err error) {
r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host}
// set legacy watcher
if c.Bool("legacy"){
r.Settings.Legacy.Set(1)
r.Settings.Legacy.Set(c.Bool("legacy"),1)
}
// set server
if c.Bool("server"){
r.Server.Set(c.Bool("server"), c.Bool("open"),realize.Port,realize.Host)
}
// check no-config and read
if !c.Bool("no-config") {
@ -1138,18 +1141,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
}
}
// check project list length
if len(r.Schema.Projects) <= 0 {
// create a new project based on given params
@ -1164,6 +1155,18 @@ func start(c *cli.Context) (err error) {
}
}
}
// Start web server
if r.Server.Status {
r.Server.Parent = &r
err = r.Server.Start()
if err != nil {
return err
}
err = r.Server.OpenURL()
if err != nil {
return err
}
}
// start workflow
return r.Start()
}

View File

@ -9,12 +9,12 @@ import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"golang.org/x/net/websocket"
"io"
"log"
"net/http"
"os/exec"
"runtime"
"strconv"
"github.com/go-siris/siris/core/errors"
)
// Dafault host and port
@ -98,65 +98,75 @@ func (s *Server) render(c echo.Context, path string, mime int) error {
return nil
}
func (s *Server) Set(status bool, open bool, port int, host string) {
s.Open = open
s.Port = port
s.Host = host
s.Status = status
}
// Start the web server
func (s *Server) Start() (err error) {
e := echo.New()
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 2,
}))
e.Use(middleware.Recover())
println(s.Status,"start")
if s.Status {
e := echo.New()
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 2,
}))
e.Use(middleware.Recover())
// web panel
e.GET("/", func(c echo.Context) error {
return s.render(c, "assets/index.html", 1)
})
e.GET("/assets/js/all.min.js", func(c echo.Context) error {
return s.render(c, "assets/assets/js/all.min.js", 2)
})
e.GET("/assets/css/app.css", func(c echo.Context) error {
return s.render(c, "assets/assets/css/app.css", 3)
})
e.GET("/app/components/settings/index.html", func(c echo.Context) error {
return s.render(c, "assets/app/components/settings/index.html", 1)
})
e.GET("/app/components/project/index.html", func(c echo.Context) error {
return s.render(c, "assets/app/components/project/index.html", 1)
})
e.GET("/app/components/index.html", func(c echo.Context) error {
return s.render(c, "assets/app/components/index.html", 1)
})
e.GET("/assets/img/logo.png", func(c echo.Context) error {
return s.render(c, "assets/assets/img/logo.png", 5)
})
e.GET("/assets/img/svg/github-logo.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/github-logo.svg", 4)
})
e.GET("/assets/img/svg/ic_arrow_back_black_48px.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/ic_arrow_back_black_48px.svg", 4)
})
e.GET("/assets/img/svg/ic_clear_white_48px.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/ic_clear_white_48px.svg", 4)
})
e.GET("/assets/img/svg/ic_menu_white_48px.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/ic_menu_white_48px.svg", 4)
})
e.GET("/assets/img/svg/ic_settings_black_48px.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/ic_settings_black_48px.svg", 4)
})
// web panel
e.GET("/", func(c echo.Context) error {
return s.render(c, "assets/index.html", 1)
})
e.GET("/assets/js/all.min.js", func(c echo.Context) error {
return s.render(c, "assets/assets/js/all.min.js", 2)
})
e.GET("/assets/css/app.css", func(c echo.Context) error {
return s.render(c, "assets/assets/css/app.css", 3)
})
e.GET("/app/components/settings/index.html", func(c echo.Context) error {
return s.render(c, "assets/app/components/settings/index.html", 1)
})
e.GET("/app/components/project/index.html", func(c echo.Context) error {
return s.render(c, "assets/app/components/project/index.html", 1)
})
e.GET("/app/components/index.html", func(c echo.Context) error {
return s.render(c, "assets/app/components/index.html", 1)
})
e.GET("/assets/img/logo.png", func(c echo.Context) error {
return s.render(c, "assets/assets/img/logo.png", 5)
})
e.GET("/assets/img/svg/github-logo.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/github-logo.svg", 4)
})
e.GET("/assets/img/svg/ic_arrow_back_black_48px.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/ic_arrow_back_black_48px.svg", 4)
})
e.GET("/assets/img/svg/ic_clear_white_48px.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/ic_clear_white_48px.svg", 4)
})
e.GET("/assets/img/svg/ic_menu_white_48px.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/ic_menu_white_48px.svg", 4)
})
e.GET("/assets/img/svg/ic_settings_black_48px.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/ic_settings_black_48px.svg", 4)
})
//websocket
e.GET("/ws", s.projects)
e.HideBanner = true
e.Debug = false
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))
}()
//websocket
e.GET("/ws", s.projects)
e.HideBanner = true
e.Debug = false
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))
}()
}
return nil
}
// OpenURL in a new tab of default browser
func (s *Server) OpenURL() (io.Writer, error) {
func (s *Server) OpenURL() error {
url := "http://" + string(s.Parent.Server.Host) + ":" + strconv.Itoa(s.Parent.Server.Port)
stderr := bytes.Buffer{}
cmd := map[string]string{
@ -167,13 +177,13 @@ func (s *Server) OpenURL() (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)
return 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 errors.New(stderr.String())
}
}
return nil, nil
return nil
}

View File

@ -62,16 +62,11 @@ type Resource struct {
}
// Set legacy watcher with an interval
func (l *Legacy) Set(interval int){
func (l *Legacy) Set(status bool, interval int){
l.Force = true
l.Interval = time.Duration(interval) * time.Second
}
// Unset legacy watcher
func (l *Legacy) Unset(){
l.Force = false
}
// Remove realize folder
func (s *Settings) Remove(d string) error {
_, err := os.Stat(d)