package main
import (
"html/template"
"io"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)
type Templates struct {
templates *template.Template
}
func (t *Templates) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func NewTemplates() *Templates {
return &Templates{
templates: template.Must(template.ParseGlob("views/*.html")),
}
}
type Page struct {
Boxes map[int]bool
}
func newPage(boxes map[int]bool) Page {
return Page{
Boxes: boxes,
}
}
var (
e = echo.New()
)
func main() {
e.Renderer = NewTemplates()
e.Logger.SetLevel(log.DEBUG)
e.Use(middleware.Logger())
e.GET("/health", healthCheck)
e.Static("/css", "css")
e.Static("/js", "js")
e.GET("/", func(c echo.Context) error {
return c.Render(200, "index", newPage(getBoxes()))
})
e.GET("/ws", initWs)
e.Logger.Fatal(e.Start(":54321"))
defer db.Close()
}
func healthCheck(c echo.Context) error {
return c.String(200, "OK")
}