mirror of
https://github.com/mainnika/nikita-tokarch-uk.git
synced 2026-05-25 01:03:35 +00:00
Merge branch 'develop' into main
This commit is contained in:
+30
-4
@@ -1,23 +1,49 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"bytes"
|
||||||
|
|
||||||
routing "github.com/jackwhelpton/fasthttp-routing/v2"
|
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/content"
|
||||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params"
|
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params"
|
||||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates"
|
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates"
|
||||||
)
|
)
|
||||||
|
|
||||||
// rootRedirect redirects the root url to the index using http redirect
|
// relativeRedirectBytes makes a relative redirect by using http Location header
|
||||||
func (r *Routes) rootRedirect(c *routing.Context) (err error) {
|
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
|
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 = append(fullPath, '.')
|
||||||
|
fullPath = append(fullPath, []byte(templates.URLPostfix)...)
|
||||||
|
|
||||||
|
return r.relativeRedirectBytes(c, fullPath, fasthttp.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
// index handler renders index data
|
// index handler renders index data
|
||||||
func (r *Routes) index(c *routing.Context) (err error) {
|
func (r *Routes) index(c *routing.Context) (err error) {
|
||||||
|
|
||||||
|
|||||||
@@ -37,12 +37,14 @@ func (r *Routes) init() {
|
|||||||
|
|
||||||
router.Use(r.useTemplateWriter)
|
router.Use(r.useTemplateWriter)
|
||||||
router.Use(r.useErrorHandler)
|
router.Use(r.useErrorHandler)
|
||||||
|
router.Use(r.usePostfixForce)
|
||||||
router.NotFound(r.errorNotFound)
|
router.NotFound(r.errorNotFound)
|
||||||
|
|
||||||
root := router.Group(r.Base)
|
root := router.Group(r.Base)
|
||||||
root.Get(templates.URLRoot, r.rootRedirect)
|
root.Get(templates.URLRoot, r.rootRedirect)
|
||||||
root.Get(templates.URLIndex, r.index)
|
root.Get(templates.URLIndex, r.index)
|
||||||
root.Get(templates.URLBlog, r.blog)
|
root.Get(templates.URLBlog, r.blog)
|
||||||
|
root.Get(templates.URLSlug, r.slug)
|
||||||
|
|
||||||
r.router = router
|
r.router = router
|
||||||
r.handler = router.HandleRequest
|
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
|
package templates
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
URLPostfix = "aspx"
|
||||||
|
|
||||||
URLRoot = "/"
|
URLRoot = "/"
|
||||||
URLIndex = "/index.aspx"
|
URLSlug = "/<slug:[^/\\.]*>." + URLPostfix
|
||||||
URLBlog = "/blog.aspx"
|
URLIndex = "/index." + URLPostfix
|
||||||
URLPost = "/post.aspx"
|
URLBlog = "/blog." + URLPostfix
|
||||||
|
URLPost = "/post." + URLPostfix
|
||||||
URLJSApp = "/js-bin/app.js"
|
URLJSApp = "/js-bin/app.js"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user