realize/server/main.go

63 lines
1.3 KiB
Go
Raw Normal View History

2016-09-01 22:35:31 +00:00
package server
import (
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 13:15:11 +00:00
c "github.com/tockins/realize/cli"
"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 13:15:11 +00:00
var Bp *c.Blueprint
// Server struct contains server informations
type Server struct {
}
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
}
// Server starting
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-06 00:31:44 +00:00
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 13:15:11 +00:00
// The WebSocket for projects list
func projects() websocket.Handler {
return websocket.Handler(func(ws *websocket.Conn) {
2016-09-17 13:15:11 +00:00
for {
2016-09-17 13:33:07 +00:00
message, _ := json.Marshal(Bp)
err := websocket.Message.Send(ws, string(message))
fmt.Println(Bp)
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)
}
}
})
}