From 8f3e81fac370611836da043ed7585cec76de97a8 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 26 Jan 2022 04:24:48 +0100 Subject: [PATCH 1/3] Always use relative redirect to keep the upstream domain --- pkg/routes/index.go | 15 +++++++++++---- pkg/templates/urls.go | 8 +++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/routes/index.go b/pkg/routes/index.go index c3f654b..c002abc 100644 --- a/pkg/routes/index.go +++ b/pkg/routes/index.go @@ -1,23 +1,30 @@ 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" ) -// rootRedirect redirects the root url to the index using http redirect -func (r *Routes) rootRedirect(c *routing.Context) (err error) { +// relativeRedirectBytes makes a relative redirect by using http Location header +func (r *Routes) relativeRedirectBytes(c *routing.Context, location []byte, statusCode int) (err error) { - c.Redirect(templates.URLIndex, http.StatusFound) + 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) +} + // index handler renders index data func (r *Routes) index(c *routing.Context) (err error) { diff --git a/pkg/templates/urls.go b/pkg/templates/urls.go index 29e7f56..d2c931d 100644 --- a/pkg/templates/urls.go +++ b/pkg/templates/urls.go @@ -1,9 +1,11 @@ package templates const ( + URLPostfix = "aspx" + URLRoot = "/" - URLIndex = "/index.aspx" - URLBlog = "/blog.aspx" - URLPost = "/post.aspx" + URLIndex = "/index." + URLPostfix + URLBlog = "/blog." + URLPostfix + URLPost = "/post." + URLPostfix URLJSApp = "/js-bin/app.js" ) From 1e6cbd574f90ea33b80fd71bb1a36ff3e5353062 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 26 Jan 2022 04:25:20 +0100 Subject: [PATCH 2/3] Always redirect to a postfixed path --- pkg/routes/index.go | 19 +++++++++++++++++++ pkg/routes/routes.go | 1 + 2 files changed, 20 insertions(+) diff --git a/pkg/routes/index.go b/pkg/routes/index.go index c002abc..ffeabea 100644 --- a/pkg/routes/index.go +++ b/pkg/routes/index.go @@ -25,6 +25,25 @@ func (r *Routes) rootRedirect(c *routing.Context) (err error) { return r.relativeRedirectBytes(c, []byte(templates.URLIndex), fasthttp.StatusFound) } +// rootRedirect forcefully adds postfix to the url +func (r *Routes) usePostfixForce(c *routing.Context) (err error) { + + 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 func (r *Routes) index(c *routing.Context) (err error) { diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index f2a8a3b..2be16a0 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -37,6 +37,7 @@ 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) From dfe60f241ada515bd3fdcd6b0fc6dac952f1f20e Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 26 Jan 2022 04:46:18 +0100 Subject: [PATCH 3/3] 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