mirror of
https://github.com/mainnika/nikita-tokarch-uk.git
synced 2026-05-25 01:03:35 +00:00
Restructure the go package
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
routing "github.com/jackwhelpton/fasthttp-routing/v2"
|
||||
|
||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content"
|
||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params"
|
||||
)
|
||||
|
||||
// blog handler renders blog data
|
||||
func (r *Routes) blog(c *routing.Context) (err error) {
|
||||
|
||||
postsPerPage := r.ContentConfig.PostsPerPage
|
||||
currentPage := c.QueryArgs().GetUintOrZero("page")
|
||||
|
||||
latestPosts, err := r.GhostClient.GetPosts(
|
||||
params.WithLimit(postsPerPage),
|
||||
params.WithPage(currentPage),
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
blogContent := content.Blog{
|
||||
Meta: latestPosts.Meta,
|
||||
Posts: latestPosts.Posts,
|
||||
}
|
||||
|
||||
return c.Write(blogContent)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
routing "github.com/jackwhelpton/fasthttp-routing/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content"
|
||||
)
|
||||
|
||||
// errorNotFound renders http error-404 template
|
||||
func (r *Routes) errorNotFound(c *routing.Context) (err error) {
|
||||
|
||||
errorContent := content.Error{Message: "not found"}
|
||||
|
||||
return c.Write(errorContent)
|
||||
}
|
||||
|
||||
// useErrorHandler is the middleware that catch handlers errors and render error template
|
||||
func (r *Routes) useErrorHandler(c *routing.Context) (err error) {
|
||||
|
||||
worker := func() (err error) {
|
||||
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = routing.NewHTTPError(http.StatusInternalServerError,
|
||||
fmt.Sprintf("panic:\n%v", r))
|
||||
}()
|
||||
|
||||
err = c.Next()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = worker()
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
c.Abort()
|
||||
|
||||
logrus.Warnf("Cannot process request, %v", err)
|
||||
|
||||
errorContent := content.Error{
|
||||
Message: err.Error(),
|
||||
}
|
||||
|
||||
return c.Write(errorContent)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
routing "github.com/jackwhelpton/fasthttp-routing/v2"
|
||||
|
||||
"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) {
|
||||
|
||||
c.Redirect(templates.URLIndex, http.StatusFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
routing "github.com/jackwhelpton/fasthttp-routing/v2"
|
||||
"github.com/valyala/fasthttp"
|
||||
|
||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates"
|
||||
)
|
||||
|
||||
var _ routing.DataWriter = (*TemplateWriter)(nil)
|
||||
|
||||
// staticWriter is thread-safe static instance of template writer
|
||||
var staticWriter = &TemplateWriter{}
|
||||
|
||||
// TemplateWriter is the fasthttp data writer that loads and executes template using the content
|
||||
type TemplateWriter struct{}
|
||||
|
||||
// SetHeader sets the content type to HTML since all templates are HTML
|
||||
func (tw *TemplateWriter) SetHeader(rh *fasthttp.ResponseHeader) {
|
||||
rh.SetContentType(routing.MIME_HTML)
|
||||
}
|
||||
|
||||
// Write executes the template and writes result to the response writer
|
||||
func (tw *TemplateWriter) Write(w io.Writer, content interface{}) error {
|
||||
|
||||
template := templates.GetTemplateOf(content)
|
||||
|
||||
return template.Execute(w, content)
|
||||
}
|
||||
|
||||
// useTemplateWriter is the routing middleware to set the default data writer
|
||||
func (r *Routes) useTemplateWriter(c *routing.Context) (err error) {
|
||||
|
||||
c.SetDataWriter(staticWriter)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
routing "github.com/jackwhelpton/fasthttp-routing/v2"
|
||||
"github.com/valyala/fasthttp"
|
||||
|
||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config"
|
||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost"
|
||||
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates"
|
||||
)
|
||||
|
||||
// Routes is the main handler that contains all routes handlers
|
||||
type Routes struct {
|
||||
GhostClient ghost.Client
|
||||
ContentConfig config.Content
|
||||
|
||||
Base string
|
||||
|
||||
router *routing.Router
|
||||
handler fasthttp.RequestHandler
|
||||
|
||||
initOnce sync.Once
|
||||
}
|
||||
|
||||
// Handler invokes the lazy once-initializer and then does the request
|
||||
func (r *Routes) Handler(ctx *fasthttp.RequestCtx) {
|
||||
r.initOnce.Do(r.init)
|
||||
r.handler(ctx)
|
||||
}
|
||||
|
||||
// init has the renderer initialization
|
||||
func (r *Routes) init() {
|
||||
|
||||
router := routing.New()
|
||||
|
||||
router.Use(r.useTemplateWriter)
|
||||
router.Use(r.useErrorHandler)
|
||||
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)
|
||||
|
||||
r.router = router
|
||||
r.handler = router.HandleRequest
|
||||
}
|
||||
Reference in New Issue
Block a user