Handle custom urls by using ghost-slug id

main
Nikita Tokarchuk 2 years ago
parent 1e6cbd574f
commit dfe60f241a
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 1
      pkg/routes/routes.go
  2. 30
      pkg/routes/slug.go
  3. 1
      pkg/templates/urls.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

@ -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)
}

@ -4,6 +4,7 @@ const (
URLPostfix = "aspx"
URLRoot = "/"
URLSlug = "/<slug:[^/\\.]*>." + URLPostfix
URLIndex = "/index." + URLPostfix
URLBlog = "/blog." + URLPostfix
URLPost = "/post." + URLPostfix

Loading…
Cancel
Save