foo
This commit is contained in:
parent
fb5bfbc100
commit
506fbb575c
6 changed files with 192 additions and 6 deletions
58
server.go
58
server.go
|
@ -1,6 +1,10 @@
|
|||
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"
|
||||
|
@ -8,9 +12,59 @@ import (
|
|||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
e.Static("/", "static")
|
||||
e.GET("/", func(c echo.Context) error {
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue