realize/server/main.go

109 lines
2.8 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"
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-18 11:50:15 +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"
)
// Server struct contains server informations
2016-09-18 11:50:15 +00:00
type Server struct {
2016-09-17 23:04:36 +00:00
Blueprint *c.Blueprint
2016-09-18 11:50:15 +00:00
Files map[string]string
Sync chan string
}
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
}
2016-09-06 00:31:44 +00:00
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-10-14 09:03:58 +00:00
// web panel
2016-09-01 22:35:31 +00:00
e.GET("/", func(c echo.Context) error {
2016-10-14 09:03:58 +00:00
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)
})
e.GET("/app/components/projects/index.html", func(c echo.Context) error {
return render(c, "assets/app/components/projects/index.html", 1)
})
e.GET("/app/components/project/index.html", func(c echo.Context) error {
return render(c, "assets/app/components/project/index.html", 1)
2016-09-01 22:35:31 +00:00
})
2016-10-14 09:03:58 +00:00
e.GET("/app/components/index.html", func(c echo.Context) error {
return render(c, "assets/app/components/index.html", 1)
})
e.GET("/assets/img/svg/github-logo.svg", func(c echo.Context) error {
return render(c, "assets/assets/img/svg/github-logo.svg", 4)
})
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)
})
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)
})
//websocket
e.GET("/ws", standard.WrapHandler(s.projects()))
2016-09-01 22:35:31 +00:00
go e.Run(standard.New(":5000"))
2016-10-14 09:03:58 +00:00
Open("http://localhost:5000")
2016-09-01 22:35:31 +00:00
}
2016-09-17 13:15:11 +00:00
// The WebSocket for projects list
func (s *Server) projects() websocket.Handler {
return websocket.Handler(func(ws *websocket.Conn) {
msg := func() {
2016-10-14 09:03:58 +00:00
message, _ := json.Marshal(s.Blueprint.Projects)
2016-09-17 13:33:07 +00:00
err := websocket.Message.Send(ws, string(message))
2016-09-17 13:15:11 +00:00
if err != nil {
log.Fatal(err)
}
}
msg()
for {
select {
default:
continue
case <-s.Sync:
msg()
2016-09-17 13:15:11 +00:00
}
}
})
}