2017-09-20 13:56:31 +00:00
|
|
|
package main
|
2016-09-01 22:35:31 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-08 21:09:45 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-09-01 22:35:31 +00:00
|
|
|
"github.com/labstack/echo"
|
2016-09-06 00:31:44 +00:00
|
|
|
"github.com/labstack/echo/middleware"
|
2017-09-20 13:56:31 +00:00
|
|
|
"io"
|
2017-10-08 21:09:45 +00:00
|
|
|
"net/http"
|
2017-09-20 13:56:31 +00:00
|
|
|
"os/exec"
|
2017-10-08 21:09:45 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2016-09-01 22:35:31 +00:00
|
|
|
)
|
|
|
|
|
2017-09-03 18:01:56 +00:00
|
|
|
// Dafault host and port
|
|
|
|
const (
|
2017-11-19 23:52:31 +00:00
|
|
|
Host = "localhost"
|
|
|
|
Port = 5001
|
2017-09-03 18:01:56 +00:00
|
|
|
)
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Server settings
|
2017-10-08 21:09:45 +00:00
|
|
|
type Server struct {
|
2017-11-19 23:52:31 +00:00
|
|
|
parent *Realize
|
2017-10-08 21:09:45 +00:00
|
|
|
Status bool `yaml:"status" json:"status"`
|
|
|
|
Open bool `yaml:"open" json:"open"`
|
|
|
|
Port int `yaml:"port" json:"port"`
|
2017-11-12 21:42:21 +00:00
|
|
|
Host string `yaml:"host" json:"host"`
|
2016-09-17 12:38:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-08 21:09:45 +00:00
|
|
|
// Websocket projects
|
2017-11-12 21:42:21 +00:00
|
|
|
func (s *Server) projects(c echo.Context) (err error) {
|
2017-11-19 23:52:31 +00:00
|
|
|
//websocket.Handler(func(ws *websocket.Conn) {
|
|
|
|
// msg, _ := json.Marshal(s.parent)
|
|
|
|
// err = websocket.Message.Send(ws, string(msg))
|
|
|
|
// go func() {
|
|
|
|
// for {
|
|
|
|
// select {
|
|
|
|
// case <-s.parent.sync:
|
|
|
|
// msg, _ := json.Marshal(s.parent)
|
|
|
|
// err = websocket.Message.Send(ws, string(msg))
|
|
|
|
// if err != nil {
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }()
|
|
|
|
// for {
|
|
|
|
// // Read
|
|
|
|
// text := ""
|
|
|
|
// err = websocket.Message.Receive(ws, &text)
|
|
|
|
// if err != nil {
|
|
|
|
// break
|
|
|
|
// } else {
|
|
|
|
// err := json.Unmarshal([]byte(text), &s.parent)
|
|
|
|
// if err == nil {
|
|
|
|
// s.parent.Settings.record(s.parent)
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ws.Close()
|
|
|
|
//}).ServeHTTP(c.Response(), c.Request())
|
2016-09-06 00:31:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-08 21:09:45 +00:00
|
|
|
// Render return a web pages defined in bindata
|
|
|
|
func (s *Server) render(c echo.Context, path string, mime int) error {
|
|
|
|
data, err := Asset(path)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
rs := c.Response()
|
|
|
|
// check content type by extensions
|
|
|
|
switch mime {
|
|
|
|
case 1:
|
|
|
|
rs.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
|
|
|
|
break
|
|
|
|
case 2:
|
|
|
|
rs.Header().Set(echo.HeaderContentType, echo.MIMEApplicationJavaScriptCharsetUTF8)
|
|
|
|
break
|
|
|
|
case 3:
|
|
|
|
rs.Header().Set(echo.HeaderContentType, "text/css")
|
|
|
|
break
|
|
|
|
case 4:
|
|
|
|
rs.Header().Set(echo.HeaderContentType, "image/svg+xml")
|
|
|
|
break
|
|
|
|
case 5:
|
|
|
|
rs.Header().Set(echo.HeaderContentType, "image/png")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rs.WriteHeader(http.StatusOK)
|
|
|
|
rs.Write(data)
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-19 23:52:31 +00:00
|
|
|
|
|
|
|
// 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())
|
|
|
|
|
|
|
|
// 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/svg/settings.svg", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/svg/settings.svg", 4)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/svg/fullscreen.svg", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/svg/fullscreen.svg", 4)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/svg/add.svg", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/svg/add.svg", 4)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/svg/backspace.svg", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/svg/backspace.svg", 4)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/svg/error.svg", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/svg/error.svg", 4)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/svg/remove.svg", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/remove.svg", 4)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/svg/logo.svg", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/svg/logo.svg", 4)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/fav.png", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/fav.png", 5)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/svg/circle.svg", func(c echo.Context) error {
|
|
|
|
return s.render(c, "assets/assets/img/svg/circle.svg", 4)
|
|
|
|
})
|
|
|
|
|
|
|
|
//websocket
|
|
|
|
e.GET("/ws", s.projects)
|
|
|
|
e.HideBanner = true
|
|
|
|
e.Debug = false
|
|
|
|
go e.Start(string(s.parent.Server.Host) + ":" + strconv.Itoa(s.parent.Server.Port))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenURL in a new tab of default browser
|
|
|
|
func (s *Server) OpenURL() (io.Writer, error) {
|
|
|
|
url := "http://" + string(s.parent.Server.Host) + ":" + strconv.Itoa(s.parent.Server.Port)
|
|
|
|
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
|
|
|
|
}
|