2024-06-09 09:31:30 -07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-06-15 12:34:24 -07:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-06-15 12:34:24 -07:00
|
|
|
"github.com/mikestefanello/pagoda/pkg/htmx"
|
2024-06-09 09:31:30 -07:00
|
|
|
"github.com/mikestefanello/pagoda/pkg/services"
|
|
|
|
)
|
|
|
|
|
|
|
|
var handlers []Handler
|
|
|
|
|
|
|
|
// Handler handles one or more HTTP routes
|
|
|
|
type Handler interface {
|
|
|
|
// Routes allows for self-registration of HTTP routes on the router
|
|
|
|
Routes(g *echo.Group)
|
|
|
|
|
|
|
|
// Init provides the service container to initialize
|
|
|
|
Init(*services.Container) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers a handler
|
|
|
|
func Register(h Handler) {
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandlers returns all handlers
|
|
|
|
func GetHandlers() []Handler {
|
|
|
|
return handlers
|
|
|
|
}
|
2024-06-15 12:34:24 -07:00
|
|
|
|
|
|
|
// redirect redirects to a given route name with optional route parameters
|
|
|
|
func redirect(ctx echo.Context, route string, routeParams ...any) error {
|
|
|
|
url := ctx.Echo().Reverse(route, routeParams...)
|
|
|
|
|
|
|
|
if htmx.GetRequest(ctx).Boosted {
|
|
|
|
htmx.Response{
|
|
|
|
Redirect: url,
|
|
|
|
}.Apply(ctx)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return ctx.Redirect(http.StatusFound, url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail is a helper to fail a request by returning a 500 error and logging the error
|
|
|
|
func fail(err error, log string) error {
|
|
|
|
// The error handler will handle logging
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("%s: %v", log, err))
|
|
|
|
}
|