71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/labstack/echo-contrib/echoprometheus"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
"golang.org/x/net/websocket"
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func main() {
|
|
e := echo.New()
|
|
e.Use(middleware.Logger())
|
|
e.Use(middleware.Recover())
|
|
e.Use(middleware.Gzip())
|
|
e.Pre(middleware.RemoveTrailingSlash())
|
|
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
Root: "static",
|
|
HTML5: true, // SPA mode; not-found will be redirected to root
|
|
}))
|
|
e.Use(echoprometheus.NewMiddleware("sanic")) // adds middleware to gather metrics
|
|
|
|
e.GET("/metrics", echoprometheus.NewHandler())
|
|
|
|
e.GET("/", func(c echo.Context) (err error) {
|
|
pusher, ok := c.Response().Writer.(http.Pusher)
|
|
if ok {
|
|
if err = pusher.Push("/style.css", nil); err != nil {
|
|
return
|
|
}
|
|
if err = pusher.Push("/index.js", nil); err != nil {
|
|
return
|
|
}
|
|
if err = pusher.Push("/favicon.ico", nil); err != nil {
|
|
return
|
|
}
|
|
}
|
|
return c.File("index.html")
|
|
})
|
|
|
|
e.GET("/ws", wsServe)
|
|
e.GET("/api/", func(c echo.Context) error {
|
|
return c.String(http.StatusOK, "Hello, World!")
|
|
})
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
}
|
|
|
|
func wsServe(c echo.Context) error {
|
|
websocket.Handler(func(ws *websocket.Conn) {
|
|
defer ws.Close()
|
|
for {
|
|
// Write
|
|
err := websocket.Message.Send(ws, "Hello, Client!")
|
|
if err != nil {
|
|
c.Logger().Error(err)
|
|
}
|
|
|
|
// Read
|
|
msg := ""
|
|
err = websocket.Message.Receive(ws, &msg)
|
|
if err != nil {
|
|
c.Logger().Error(err)
|
|
}
|
|
fmt.Printf("%s\n", msg)
|
|
}
|
|
}).ServeHTTP(c.Response(), c.Request())
|
|
return nil
|
|
}
|