From dfe60f241ada515bd3fdcd6b0fc6dac952f1f20e Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 26 Jan 2022 04:46:18 +0100 Subject: [PATCH] Handle custom urls by using ghost-slug id --- pkg/routes/routes.go | 1 + pkg/routes/slug.go | 30 ++++++++++++++++++++++++++++++ pkg/templates/urls.go | 1 + 3 files changed, 32 insertions(+) create mode 100644 pkg/routes/slug.go diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 2be16a0..5922302 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -44,6 +44,7 @@ func (r *Routes) init() { 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 d2c931d..d7fc349 100644 --- a/pkg/templates/urls.go +++ b/pkg/templates/urls.go @@ -4,6 +4,7 @@ const ( URLPostfix = "aspx" URLRoot = "/" + URLSlug = "/." + URLPostfix URLIndex = "/index." + URLPostfix URLBlog = "/blog." + URLPostfix URLPost = "/post." + URLPostfix