realize/server.go

194 lines
5.0 KiB
Go
Raw Normal View History

2017-09-20 13:56:31 +00:00
package main
2016-09-01 22:35:31 +00:00
import (
"bytes"
2016-09-17 13:33:07 +00:00
"encoding/json"
"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"
"golang.org/x/net/websocket"
2016-11-13 14:03:19 +00:00
"gopkg.in/urfave/cli.v2"
2017-09-20 13:56:31 +00:00
"io"
"net/http"
2017-09-20 13:56:31 +00:00
"os/exec"
"runtime"
"strconv"
2016-09-01 22:35:31 +00:00
)
2017-09-03 18:01:56 +00:00
// Dafault host and port
const (
2017-09-20 13:56: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
type Server struct {
parent *realize
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"`
}
// Websocket projects
2017-11-12 21:42:21 +00:00
func (s *Server) projects(c echo.Context) (err error) {
websocket.Handler(func(ws *websocket.Conn) {
2017-10-15 19:36:14 +00:00
msg, _ := json.Marshal(s.parent)
2017-11-12 21:42:21 +00:00
err = websocket.Message.Send(ws, string(msg))
go func() {
for {
select {
case <-s.parent.sync:
2017-10-15 19:36:14 +00:00
msg, _ := json.Marshal(s.parent)
err = websocket.Message.Send(ws, string(msg))
if err != nil {
break
}
}
}
}()
for {
// Read
text := ""
2017-11-12 21:42:21 +00:00
err = websocket.Message.Receive(ws, &text)
if err != nil {
break
} else {
2017-10-15 19:36:14 +00:00
err := json.Unmarshal([]byte(text), &s.parent)
if err == nil {
s.parent.Settings.record(s.parent)
break
}
}
}
2017-11-12 21:42:21 +00:00
ws.Close()
}).ServeHTTP(c.Response(), c.Request())
2016-09-06 00:31:44 +00:00
return nil
}
2016-12-17 10:43:27 +00:00
// Start the web server
func (s *Server) start(p *cli.Context) (err error) {
2017-08-07 23:33:31 +00:00
if p.Bool("server") {
s.parent.Server.Status = p.Bool("server")
s.parent.Server.Open = true
2017-08-07 23:07:18 +00:00
}
if s.parent.Server.Status {
2016-11-01 09:56:12 +00:00
e := echo.New()
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
2017-03-12 22:53:15 +00:00
Level: 2,
}))
2016-11-20 16:58:52 +00:00
e.Use(middleware.Recover())
2016-10-14 09:03:58 +00:00
2016-11-01 09:56:12 +00:00
// web panel
e.GET("/", func(c echo.Context) error {
2017-09-20 13:56:31 +00:00
return s.render(c, "assets/index.html", 1)
2016-11-01 09:56:12 +00:00
})
e.GET("/assets/js/all.min.js", func(c echo.Context) error {
2017-09-20 13:56:31 +00:00
return s.render(c, "assets/assets/js/all.min.js", 2)
2016-11-01 09:56:12 +00:00
})
e.GET("/assets/css/app.css", func(c echo.Context) error {
2017-09-20 13:56:31 +00:00
return s.render(c, "assets/assets/css/app.css", 3)
2016-11-01 09:56:12 +00:00
})
2017-08-07 19:53:08 +00:00
e.GET("/app/components/settings/index.html", func(c echo.Context) error {
2017-09-20 13:56:31 +00:00
return s.render(c, "assets/app/components/settings/index.html", 1)
2016-11-01 09:56:12 +00:00
})
e.GET("/app/components/project/index.html", func(c echo.Context) error {
2017-09-20 13:56:31 +00:00
return s.render(c, "assets/app/components/project/index.html", 1)
2016-11-01 09:56:12 +00:00
})
e.GET("/app/components/index.html", func(c echo.Context) error {
2017-09-20 13:56:31 +00:00
return s.render(c, "assets/app/components/index.html", 1)
2016-11-01 09:56:12 +00:00
})
2017-11-05 21:38:26 +00:00
e.GET("/assets/img/svg/settings.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/settings.svg", 4)
2017-08-07 19:53:08 +00:00
})
2017-11-05 21:38:26 +00:00
e.GET("/assets/img/svg/fullscreen.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/fullscreen.svg", 4)
2017-08-07 19:53:08 +00:00
})
2017-11-05 21:38:26 +00:00
e.GET("/assets/img/svg/add.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/add.svg", 4)
2017-08-07 19:53:08 +00:00
})
2017-11-05 21:38:26 +00:00
e.GET("/assets/img/svg/backspace.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/backspace.svg", 4)
2016-11-01 09:56:12 +00:00
})
2017-11-05 21:38:26 +00:00
e.GET("/assets/img/svg/error.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/error.svg", 4)
2016-11-01 09:56:12 +00:00
})
2017-11-05 21:38:26 +00:00
e.GET("/assets/img/svg/remove.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/remove.svg", 4)
2017-08-07 19:53:08 +00:00
})
e.GET("/assets/img/svg/logo.svg", func(c echo.Context) error {
2017-09-20 13:56:31 +00:00
return s.render(c, "assets/assets/img/svg/logo.svg", 4)
2017-08-07 19:53:08 +00:00
})
2017-11-05 21:38:26 +00:00
e.GET("/assets/img/fav.png", func(c echo.Context) error {
return s.render(c, "assets/assets/img/fav.png", 5)
2017-08-07 19:53:08 +00:00
})
2017-11-05 21:38:26 +00:00
e.GET("/assets/img/svg/circle.svg", func(c echo.Context) error {
return s.render(c, "assets/assets/img/svg/circle.svg", 4)
2016-11-01 09:56:12 +00:00
})
2016-10-14 09:03:58 +00:00
2016-11-01 09:56:12 +00:00
//websocket
2016-12-17 10:43:27 +00:00
e.GET("/ws", s.projects)
2017-10-15 19:36:14 +00:00
e.HideBanner = true
2017-10-16 20:53:04 +00:00
e.Debug = false
go e.Start(string(s.parent.Server.Host) + ":" + strconv.Itoa(s.parent.Server.Port))
_, err = s.openURL("http://" + string(s.parent.Server.Host) + ":" + strconv.Itoa(s.parent.Server.Port))
2017-08-14 20:17:11 +00:00
if err != nil {
return err
2016-11-01 09:56:12 +00:00
}
}
2016-11-01 09:56:12 +00:00
return nil
2016-09-01 22:35:31 +00:00
}
2017-09-20 13:56:31 +00:00
// OpenURL in a new tab of default browser
func (s *Server) openURL(url string) (io.Writer, error) {
2017-09-20 13:56:31 +00:00
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
}
// 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
}