Implement the renderer routing basics

pull/1/head
Nikita Tokarchuk 3 years ago
parent bcb9d77de0
commit d9ab404cf2
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 6
      frontend/main.go
  2. 35
      frontend/renderer/renderer.go

@ -9,6 +9,7 @@ import (
"github.com/valyala/fasthttp"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/config"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/renderer"
_ "code.tokarch.uk/mainnika/nikita-tokarch-uk/frontend/templates"
)
@ -50,8 +51,13 @@ func main() {
logrus.Fatal(err)
}
rendererHandler := &renderer.Renderer{
Base: config.Base,
}
httpServer := fasthttp.Server{
Logger: logrus.StandardLogger(),
Handler: rendererHandler.Handler,
Name: frontendServerIdentity,
GetOnly: true,
}

@ -0,0 +1,35 @@
package renderer
import (
"sync"
routing "github.com/jackwhelpton/fasthttp-routing/v2"
"github.com/valyala/fasthttp"
)
// Renderer is the main handler that contains all routes handlers
type Renderer struct {
Base string
router *routing.Router
handler fasthttp.RequestHandler
initOnce sync.Once
}
// Handler invokes the lazy once-initializer and then does the request
func (r *Renderer) Handler(ctx *fasthttp.RequestCtx) {
r.initOnce.Do(r.init)
r.handler(ctx)
}
// init has the renderer initialization
func (r *Renderer) init() {
router := routing.New()
router.Use(r.useTemplateWriter)
r.router = router
r.handler = router.HandleRequest
}
Loading…
Cancel
Save