Merge branch 'develop' into main

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

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

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

@ -1,9 +1,12 @@
package templates
const (
URLPostfix = "aspx"
URLRoot = "/"
URLIndex = "/index.aspx"
URLBlog = "/blog.aspx"
URLPost = "/post.aspx"
URLSlug = "/<slug:[^/\\.]*>." + URLPostfix
URLIndex = "/index." + URLPostfix
URLBlog = "/blog." + URLPostfix
URLPost = "/post." + URLPostfix
URLJSApp = "/js-bin/app.js"
)

Loading…
Cancel
Save