2016-09-01 22:35:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-09-17 23:04:36 +00:00
|
|
|
c "github.com/tockins/realize/cli"
|
2016-09-17 13:33:07 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-09-01 22:35:31 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/engine/standard"
|
2016-09-06 00:31:44 +00:00
|
|
|
"github.com/labstack/echo/middleware"
|
2016-09-17 12:38:23 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2016-09-17 13:15:11 +00:00
|
|
|
"log"
|
2016-09-01 22:35:31 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2016-09-17 12:38:23 +00:00
|
|
|
// Server struct contains server informations
|
2016-09-17 23:04:36 +00:00
|
|
|
type Server struct{
|
|
|
|
Blueprint *c.Blueprint
|
2016-09-17 12:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func render(c echo.Context, path string) error {
|
2016-09-06 00:31:44 +00:00
|
|
|
data, err := Asset(path)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
rs := c.Response()
|
|
|
|
rs.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
|
|
|
|
rs.WriteHeader(http.StatusOK)
|
|
|
|
rs.Write(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-17 14:46:26 +00:00
|
|
|
// Server starting
|
2016-09-17 12:38:23 +00:00
|
|
|
func (s *Server) Start() {
|
2016-09-01 22:35:31 +00:00
|
|
|
e := echo.New()
|
2016-09-06 00:31:44 +00:00
|
|
|
e.Use(middleware.Gzip())
|
2016-09-01 22:35:31 +00:00
|
|
|
e.GET("/", func(c echo.Context) error {
|
2016-09-17 23:04:36 +00:00
|
|
|
return c.JSON(200, s.Blueprint)
|
|
|
|
//return render(c, "server/assets/index.html")
|
2016-09-01 22:35:31 +00:00
|
|
|
})
|
2016-09-17 13:15:11 +00:00
|
|
|
|
|
|
|
e.GET("/projects", standard.WrapHandler(projects()))
|
2016-09-01 22:35:31 +00:00
|
|
|
go e.Run(standard.New(":5000"))
|
|
|
|
}
|
2016-09-17 12:38:23 +00:00
|
|
|
|
2016-09-17 13:15:11 +00:00
|
|
|
// The WebSocket for projects list
|
2016-09-17 12:38:23 +00:00
|
|
|
func projects() websocket.Handler {
|
|
|
|
return websocket.Handler(func(ws *websocket.Conn) {
|
2016-09-17 13:15:11 +00:00
|
|
|
for {
|
2016-09-17 23:04:36 +00:00
|
|
|
message, _ := json.Marshal("")
|
2016-09-17 13:33:07 +00:00
|
|
|
err := websocket.Message.Send(ws, string(message))
|
2016-09-17 23:04:36 +00:00
|
|
|
fmt.Println("")
|
2016-09-17 13:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
msg := ""
|
|
|
|
err = websocket.Message.Receive(ws, &msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-09-17 12:38:23 +00:00
|
|
|
})
|
|
|
|
}
|