52 lines
1 KiB
Go
52 lines
1 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"git.wittern.io/public/goth-stack/view"
|
|
"github.com/a-h/templ"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Handler struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
func NewHandler(db *gorm.DB) Handler {
|
|
return Handler{
|
|
DB: db,
|
|
}
|
|
}
|
|
|
|
func render(ctx echo.Context, status int, t templ.Component) error {
|
|
ctx.Response().Writer.WriteHeader(status)
|
|
|
|
err := t.Render(context.Background(), ctx.Response().Writer)
|
|
if err != nil {
|
|
return ctx.String(http.StatusInternalServerError, "failed to render response template")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func AddRoutes(h Handler, e *echo.Echo) {
|
|
e.Use(middleware.Static("static"))
|
|
e.Use(middleware.Logger())
|
|
|
|
// Simple Error handler
|
|
e.HTTPErrorHandler = func(err error, c echo.Context) {
|
|
slog.Error(err.Error())
|
|
render(c, http.StatusInternalServerError, view.ErrorPage(err.Error()))
|
|
}
|
|
|
|
e.GET("/", h.Index)
|
|
}
|
|
|
|
func (h Handler) Index(c echo.Context) error {
|
|
render(c, http.StatusOK, view.Index())
|
|
return nil
|
|
}
|