79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"blueprint_fix/cmd/web"
|
|
"github.com/a-h/templ"
|
|
"io/fs"
|
|
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
func (s *Server) RegisterRoutes() http.Handler {
|
|
r := gin.Default()
|
|
|
|
r.GET("/", s.HelloWorldHandler)
|
|
|
|
r.GET("/health", s.healthHandler)
|
|
|
|
r.GET("/websocket", s.websocketHandler)
|
|
|
|
staticFiles, _ := fs.Sub(web.Files, "assets")
|
|
r.StaticFS("/assets", http.FS(staticFiles))
|
|
|
|
r.GET("/web", func(c *gin.Context) {
|
|
templ.Handler(web.HelloForm()).ServeHTTP(c.Writer, c.Request)
|
|
})
|
|
|
|
r.POST("/hello", func(c *gin.Context) {
|
|
web.HelloWebHandler(c.Writer, c.Request)
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
func (s *Server) HelloWorldHandler(c *gin.Context) {
|
|
resp := make(map[string]string)
|
|
resp["message"] = "Hello World"
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
func (s *Server) healthHandler(c *gin.Context) {
|
|
c.JSON(http.StatusOK, s.db.Health())
|
|
}
|
|
|
|
func (s *Server) websocketHandler(c *gin.Context) {
|
|
w := c.Writer
|
|
r := c.Request
|
|
socket, err := websocket.Accept(w, r, nil)
|
|
|
|
if err != nil {
|
|
log.Printf("could not open websocket: %v", err)
|
|
_, _ = w.Write([]byte("could not open websocket"))
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
defer socket.Close(websocket.StatusGoingAway, "server closing websocket")
|
|
|
|
ctx := r.Context()
|
|
socketCtx := socket.CloseRead(ctx)
|
|
|
|
for {
|
|
payload := fmt.Sprintf("server timestamp: %d", time.Now().UnixNano())
|
|
err := socket.Write(socketCtx, websocket.MessageText, []byte(payload))
|
|
if err != nil {
|
|
break
|
|
}
|
|
time.Sleep(time.Second * 2)
|
|
}
|
|
}
|