Skip to content

Commit

Permalink
✨ Fix the router middleware hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed May 8, 2024
1 parent dea38ef commit 2098eed
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 57 deletions.
6 changes: 1 addition & 5 deletions pkg/handlers/template-dir/template-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ func NewTemplateDirHandlerFromConfig(td *config.TemplateDir, options ...Template
}

func (td *TemplateDirHandler) Serve(server *server.Server, path string) error {
// TODO(manuel, 2023-05-26) This is a hack because we currently mix and match content with commands.
// The use of a middleware to handle something that could be handled by the routing framework itself
// is because gin (which really should get replaced because we actually go against its grain heavily)
// does not allow routes to overlap.
server.Router.Use(td.renderer.HandleWithTrimPrefixMiddleware(path, nil))
server.Router.GET(path+"/:page", td.renderer.WithTrimPrefixHandler("", nil))

return nil
}
3 changes: 1 addition & 2 deletions pkg/handlers/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func NewTemplateHandlerFromConfig(
}

func (t *TemplateHandler) Serve(server_ *server.Server, path string) error {

server_.Router.Pre(t.renderer.HandleWithTrimPrefixMiddleware(path, nil))
server_.Router.Pre(t.renderer.WithTrimPrefixMiddleware(path, "", nil))

return nil
}
92 changes: 45 additions & 47 deletions pkg/render/renderer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package render

import (
"github.com/go-go-golems/parka/pkg/utils"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -227,7 +228,7 @@ func (r *Renderer) Render(
return errors.Wrap(err, "error looking up template")
}
if t == nil {
return &NoPageFoundError{Page: page}
return &utils.NoPageFoundError{Page: page}
}

c.Response().WriteHeader(http.StatusOK)
Expand All @@ -240,64 +241,61 @@ func (r *Renderer) Render(
return nil
}

func (r *Renderer) HandleWithTemplateMiddleware(
path string,
templateName string,
data map[string]interface{},
) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Response().Committed {
return next(c)
}
func (r *Renderer) WithTemplateHandler(path string, templateName string, data map[string]interface{}) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Response().Committed {
return nil
}

if c.Request().URL.Path == path {
err := r.Render(c, templateName, data)
if err != nil {
if _, ok := err.(*NoPageFoundError); ok {
return next(c)
}
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
if c.Request().URL.Path == path {
err := r.Render(c, templateName, data)
if err != nil {
if _, ok := err.(*utils.NoPageFoundError); ok {
return c.NoContent(http.StatusNotFound)
}
return nil
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return next(c)
return nil
}

return c.NoContent(http.StatusOK)
}
}

func (r *Renderer) HandleWithTrimPrefixMiddleware(prefix string, data map[string]interface{}) echo.MiddlewareFunc {
func (r *Renderer) WithTemplateMiddleware(
path string,
templateName string,
data map[string]interface{},
) echo.MiddlewareFunc {
return utils.WithPathPrefixMiddleware(path, r.WithTemplateHandler(path, templateName, data))
}

func (r *Renderer) WithTrimPrefixHandler(prefix string, data map[string]interface{}) echo.HandlerFunc {
prefix = strings.TrimPrefix(prefix, "/")
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Response().Committed {
return next(c)
return func(c echo.Context) error {
rawPath := c.Request().URL.Path

if len(rawPath) > 0 && rawPath[0] == '/' {
trimmedPath := rawPath[1:]
trimmedPath = strings.TrimPrefix(trimmedPath, prefix)
if trimmedPath == "" || strings.HasSuffix(trimmedPath, "/") {
trimmedPath += "index"
}

rawPath := c.Request().URL.Path

if len(rawPath) > 0 && rawPath[0] == '/' {
trimmedPath := rawPath[1:]
trimmedPath = strings.TrimPrefix(trimmedPath, prefix)
if trimmedPath == "" || strings.HasSuffix(trimmedPath, "/") {
trimmedPath += "index"
}

err := r.Render(c, trimmedPath, data)
if err != nil {
if _, ok := err.(*NoPageFoundError); ok {
return next(c)
}
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return nil
err := r.Render(c, trimmedPath, data)
if err != nil {
return err
}

// TODO(manuel, 2024-05-07) I'm not entirely sure this is the correct way of doing things
// this is if the rawPath is empty? I'm not sure I understand the logic here
return c.NoContent(http.StatusOK)
return nil
}

// TODO(manuel, 2024-05-07) I'm not entirely sure this is the correct way of doing things
// this is if the rawPath is empty? I'm not sure I understand the logic here
return c.NoContent(http.StatusOK)
}
}

func (r *Renderer) WithTrimPrefixMiddleware(path string, prefix string, data map[string]interface{}) echo.MiddlewareFunc {
return utils.WithPathPrefixMiddleware(path, r.WithTrimPrefixHandler(prefix, data))
}
4 changes: 2 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ func (s *Server) Run(ctx context.Context) error {

// match all remaining paths to the templates
if s.DefaultRenderer != nil {
s.Router.Pre(s.DefaultRenderer.HandleWithTemplateMiddleware("/", "index", nil))
s.Router.Pre(s.DefaultRenderer.HandleWithTrimPrefixMiddleware("", nil))
s.Router.GET("/", s.DefaultRenderer.WithTemplateHandler("/", "index", nil))
s.Router.GET("/:path", s.DefaultRenderer.WithTemplateHandler("/", "index", nil))
}

addr := fmt.Sprintf("%s:%d", s.Address, s.Port)
Expand Down
2 changes: 1 addition & 1 deletion pkg/render/errors.go → pkg/utils/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package render
package utils

import "fmt"

Expand Down
30 changes: 30 additions & 0 deletions pkg/utils/http.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
package utils

import (
"github.com/labstack/echo/v4"
"net/http"
"strings"
)

type H map[string]interface{}

func WithPathPrefixMiddleware(path string, h echo.HandlerFunc) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Response().Committed {
return next(c)
}

if !strings.HasPrefix(c.Request().URL.Path, path) {
return next(c)
}

err := h(c)
if err != nil {
if _, ok := err.(*NoPageFoundError); ok {
return next(c)
}
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return nil
}
}
}

0 comments on commit 2098eed

Please sign in to comment.