2016-09-01 22:35:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-09-17 13:33:07 +00:00
|
|
|
"encoding/json"
|
2017-04-13 12:48:48 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
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-04-13 12:48:48 +00:00
|
|
|
"github.com/tockins/realize/settings"
|
|
|
|
"github.com/tockins/realize/watcher"
|
2016-09-17 12:38:23 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2016-11-13 14:03:19 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
2016-09-01 22:35:31 +00:00
|
|
|
)
|
|
|
|
|
2017-09-03 18:01:56 +00:00
|
|
|
// Dafault host and port
|
|
|
|
const (
|
|
|
|
Host = "localhost"
|
|
|
|
Port = 5001
|
|
|
|
)
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Server settings
|
2016-09-18 11:50:15 +00:00
|
|
|
type Server struct {
|
2017-04-13 12:48:48 +00:00
|
|
|
*settings.Settings `yaml:"-"`
|
|
|
|
*watcher.Blueprint `yaml:"-"`
|
|
|
|
Sync chan string `yaml:"-"`
|
2016-09-17 12:38:23 +00:00
|
|
|
}
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Render return a web pages defined in bindata
|
2016-10-14 09:03:58 +00:00
|
|
|
func render(c echo.Context, path string, mime int) error {
|
2016-09-06 00:31:44 +00:00
|
|
|
data, err := Asset(path)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
rs := c.Response()
|
2016-10-14 09:03:58 +00:00
|
|
|
// 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
|
2017-08-07 22:30:56 +00:00
|
|
|
case 5:
|
|
|
|
rs.Header().Set(echo.HeaderContentType, "image/png")
|
|
|
|
break
|
2016-10-14 09:03:58 +00:00
|
|
|
}
|
2016-09-06 00:31:44 +00:00
|
|
|
rs.WriteHeader(http.StatusOK)
|
|
|
|
rs.Write(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Start the web server
|
2016-11-01 09:56:12 +00:00
|
|
|
func (s *Server) Start(p *cli.Context) (err error) {
|
2017-08-07 23:33:31 +00:00
|
|
|
if p.Bool("server") {
|
2017-08-07 23:07:18 +00:00
|
|
|
s.Server.Status = p.Bool("server")
|
2017-09-17 22:31:52 +00:00
|
|
|
s.Server.Open = true
|
2017-08-07 23:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.Server.Status {
|
2016-11-01 09:56:12 +00:00
|
|
|
e := echo.New()
|
2017-03-12 22:46:58 +00:00
|
|
|
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
2017-03-12 22:53:15 +00:00
|
|
|
Level: 2,
|
2017-03-12 22:46:58 +00:00
|
|
|
}))
|
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 {
|
|
|
|
return 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)
|
|
|
|
})
|
|
|
|
e.GET("/assets/css/app.css", func(c echo.Context) error {
|
|
|
|
return render(c, "assets/assets/css/app.css", 3)
|
|
|
|
})
|
2017-08-07 19:53:08 +00:00
|
|
|
e.GET("/app/components/settings/index.html", func(c echo.Context) error {
|
|
|
|
return 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 {
|
|
|
|
return 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)
|
|
|
|
})
|
2017-08-07 19:53:08 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
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)
|
2016-11-01 09:56:12 +00:00
|
|
|
})
|
|
|
|
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)
|
|
|
|
})
|
2017-08-07 19:53:08 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/svg/logo.svg", func(c echo.Context) error {
|
|
|
|
return render(c, "assets/assets/img/svg/logo.svg", 4)
|
|
|
|
})
|
|
|
|
e.GET("/assets/img/favicon-32x32.png", func(c echo.Context) error {
|
2017-08-07 22:30:56 +00:00
|
|
|
return render(c, "assets/assets/img/favicon-32x32.png", 5)
|
2017-08-07 19:53:08 +00:00
|
|
|
})
|
2016-11-01 09:56:12 +00:00
|
|
|
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)
|
|
|
|
})
|
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)
|
2016-11-13 14:03:19 +00:00
|
|
|
go e.Start(string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port))
|
2017-08-14 20:17:11 +00:00
|
|
|
_, err = s.OpenURL("http://" + string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-11-01 09:56:12 +00:00
|
|
|
}
|
2016-10-17 09:44:45 +00:00
|
|
|
}
|
2016-11-01 09:56:12 +00:00
|
|
|
return nil
|
2016-09-01 22:35:31 +00:00
|
|
|
}
|
2016-09-17 12:38:23 +00:00
|
|
|
|
2017-08-14 23:13:30 +00:00
|
|
|
// Websocket projects
|
2016-12-17 10:43:27 +00:00
|
|
|
func (s *Server) projects(c echo.Context) error {
|
2016-11-20 16:58:52 +00:00
|
|
|
websocket.Handler(func(ws *websocket.Conn) {
|
|
|
|
defer ws.Close()
|
|
|
|
msg, _ := json.Marshal(s.Blueprint.Projects)
|
|
|
|
err := websocket.Message.Send(ws, string(msg))
|
2016-12-18 22:02:48 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.Sync:
|
|
|
|
msg, _ := json.Marshal(s.Blueprint.Projects)
|
|
|
|
err = websocket.Message.Send(ws, string(msg))
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2016-11-20 16:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-18 22:02:48 +00:00
|
|
|
}()
|
|
|
|
for {
|
2016-11-20 16:58:52 +00:00
|
|
|
// Read
|
|
|
|
text := ""
|
|
|
|
err := websocket.Message.Receive(ws, &text)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
err := json.Unmarshal([]byte(text), &s.Blueprint.Projects)
|
|
|
|
if err != nil {
|
2017-09-17 22:31:52 +00:00
|
|
|
s.Record(s.Settings)
|
2016-11-20 16:58:52 +00:00
|
|
|
break
|
|
|
|
}
|
2016-09-17 13:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-20 16:58:52 +00:00
|
|
|
}).ServeHTTP(c.Response(), c.Request())
|
|
|
|
return nil
|
2016-09-17 12:38:23 +00:00
|
|
|
}
|