You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
nikita-tokarch-uk/pkg/routes/index.go

85 lines
2.0 KiB

package routes
import (
"bytes"
routing "github.com/jackwhelpton/fasthttp-routing/v2"
"github.com/valyala/fasthttp"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/templates"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params"
)
// 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)
}
// 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 = bytes.TrimRightFunc(fullPath, func(r rune) bool {
switch r {
case '/':
return true
default:
return false
}
})
fullPath = append(fullPath, '.')
fullPath = append(fullPath, []byte(templates.URLPostfix)...)
if c.QueryArgs().Len() > 0 {
fullPath = append(fullPath, '?')
fullPath = c.QueryArgs().AppendBytes(fullPath)
}
return r.relativeRedirectBytes(c, fullPath, fasthttp.StatusFound)
}
// index handler renders index data
func (r *Routes) index(c *routing.Context) (err error) {
pinnedPageSlug := r.ContentConfig.Pinned
postsPerPage := r.ContentConfig.PostsPerPage
pinnedPages, err := r.GhostClient.GetPageBySlug(pinnedPageSlug)
if err != nil {
return
}
latestPosts, err := r.GhostClient.GetPosts(params.WithLimit(postsPerPage))
if err != nil {
return
}
indexContent := content.Index{
Pinned: pinnedPages.Pages,
Meta: latestPosts.Meta,
Posts: latestPosts.Posts,
}
return c.Write(indexContent)
}