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.
50 lines
1.1 KiB
50 lines
1.1 KiB
3 years ago
|
package routes
|
||
3 years ago
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
routing "github.com/jackwhelpton/fasthttp-routing/v2"
|
||
|
"github.com/valyala/fasthttp"
|
||
3 years ago
|
|
||
3 years ago
|
"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"
|
||
3 years ago
|
)
|
||
|
|
||
3 years ago
|
// Routes is the main handler that contains all routes handlers
|
||
|
type Routes struct {
|
||
3 years ago
|
GhostClient ghost.Client
|
||
3 years ago
|
ContentConfig config.Content
|
||
|
|
||
3 years ago
|
Base string
|
||
|
|
||
|
router *routing.Router
|
||
|
handler fasthttp.RequestHandler
|
||
|
|
||
|
initOnce sync.Once
|
||
|
}
|
||
|
|
||
|
// Handler invokes the lazy once-initializer and then does the request
|
||
3 years ago
|
func (r *Routes) Handler(ctx *fasthttp.RequestCtx) {
|
||
3 years ago
|
r.initOnce.Do(r.init)
|
||
|
r.handler(ctx)
|
||
|
}
|
||
|
|
||
|
// init has the renderer initialization
|
||
3 years ago
|
func (r *Routes) init() {
|
||
3 years ago
|
|
||
|
router := routing.New()
|
||
|
|
||
|
router.Use(r.useTemplateWriter)
|
||
3 years ago
|
router.Use(r.useErrorHandler)
|
||
3 years ago
|
router.NotFound(r.errorNotFound)
|
||
3 years ago
|
|
||
3 years ago
|
root := router.Group(r.Base)
|
||
3 years ago
|
root.Get(templates.URLRoot, r.rootRedirect)
|
||
3 years ago
|
root.Get(templates.URLIndex, r.index)
|
||
3 years ago
|
root.Get(templates.URLBlog, r.blog)
|
||
3 years ago
|
|
||
3 years ago
|
r.router = router
|
||
|
r.handler = router.HandleRequest
|
||
|
}
|