diff --git a/pkg/routes/index.go b/pkg/routes/index.go index c3f654b..ffeabea 100644 --- a/pkg/routes/index.go +++ b/pkg/routes/index.go @@ -1,21 +1,47 @@ package routes import ( - "net/http" + "bytes" routing "github.com/jackwhelpton/fasthttp-routing/v2" + "github.com/valyala/fasthttp" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params" "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates" ) +// relativeRedirectBytes makes a relative redirect by using http Location header +func (r *Routes) relativeRedirectBytes(c *routing.Context, location []byte, statusCode int) (err error) { + + c.Response.Header.SetCanonical([]byte(fasthttp.HeaderLocation), location) + c.Response.SetStatusCode(statusCode) + + return +} + // rootRedirect redirects the root url to the index using http redirect func (r *Routes) rootRedirect(c *routing.Context) (err error) { + return r.relativeRedirectBytes(c, []byte(templates.URLIndex), fasthttp.StatusFound) +} - c.Redirect(templates.URLIndex, http.StatusFound) +// rootRedirect forcefully adds postfix to the url +func (r *Routes) usePostfixForce(c *routing.Context) (err error) { - return + fullPath := c.Path() + if len(fullPath) <= 1 { + return c.Next() + } + + dotIndex := bytes.LastIndexByte(fullPath, '.') + if dotIndex >= 0 { + return c.Next() + } + + fullPath = append(fullPath, '.') + fullPath = append(fullPath, []byte(templates.URLPostfix)...) + + return r.relativeRedirectBytes(c, fullPath, fasthttp.StatusFound) } // index handler renders index data diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index f2a8a3b..5922302 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -37,12 +37,14 @@ func (r *Routes) init() { router.Use(r.useTemplateWriter) router.Use(r.useErrorHandler) + router.Use(r.usePostfixForce) router.NotFound(r.errorNotFound) root := router.Group(r.Base) root.Get(templates.URLRoot, r.rootRedirect) root.Get(templates.URLIndex, r.index) root.Get(templates.URLBlog, r.blog) + root.Get(templates.URLSlug, r.slug) r.router = router r.handler = router.HandleRequest diff --git a/pkg/routes/slug.go b/pkg/routes/slug.go new file mode 100644 index 0000000..da217d2 --- /dev/null +++ b/pkg/routes/slug.go @@ -0,0 +1,30 @@ +package routes + +import ( + "net/http" + + routing "github.com/jackwhelpton/fasthttp-routing/v2" + + "code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content" +) + +// slug renders page by its slug +func (r *Routes) slug(c *routing.Context) (err error) { + + pageSlug := c.Param("slug") + if pageSlug == "" { + return routing.NewHTTPError(http.StatusNotFound) + } + + page, err := r.GhostClient.GetPageBySlug(pageSlug) + if err != nil { + return + } + + pageContent := content.Blog{ + Meta: page.Meta, + Posts: page.Pages, + } + + return c.Write(pageContent) +} diff --git a/pkg/templates/urls.go b/pkg/templates/urls.go index 29e7f56..d7fc349 100644 --- a/pkg/templates/urls.go +++ b/pkg/templates/urls.go @@ -1,9 +1,12 @@ package templates const ( + URLPostfix = "aspx" + URLRoot = "/" - URLIndex = "/index.aspx" - URLBlog = "/blog.aspx" - URLPost = "/post.aspx" + URLSlug = "/." + URLPostfix + URLIndex = "/index." + URLPostfix + URLBlog = "/blog." + URLPostfix + URLPost = "/post." + URLPostfix URLJSApp = "/js-bin/app.js" )