Move controller to the template renderer (#68)

This commit is contained in:
Mike Stefanello 2024-06-15 15:34:24 -04:00 committed by GitHub
parent baa391fb20
commit 8eafb6b666
26 changed files with 654 additions and 679 deletions

View file

@ -9,11 +9,11 @@ import (
"github.com/mikestefanello/pagoda/ent"
"github.com/mikestefanello/pagoda/ent/user"
"github.com/mikestefanello/pagoda/pkg/context"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/form"
"github.com/mikestefanello/pagoda/pkg/log"
"github.com/mikestefanello/pagoda/pkg/middleware"
"github.com/mikestefanello/pagoda/pkg/msg"
"github.com/mikestefanello/pagoda/pkg/page"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/templates"
)
@ -36,7 +36,7 @@ type (
auth *services.AuthClient
mail *services.MailClient
orm *ent.Client
controller.Controller
*services.TemplateRenderer
}
forgotPasswordForm struct {
@ -69,51 +69,51 @@ func init() {
Register(new(Auth))
}
func (c *Auth) Init(ct *services.Container) error {
c.Controller = controller.NewController(ct)
c.orm = ct.ORM
c.auth = ct.Auth
c.mail = ct.Mail
func (h *Auth) Init(c *services.Container) error {
h.TemplateRenderer = c.TemplateRenderer
h.orm = c.ORM
h.auth = c.Auth
h.mail = c.Mail
return nil
}
func (c *Auth) Routes(g *echo.Group) {
g.GET("/logout", c.Logout, middleware.RequireAuthentication()).Name = routeNameLogout
g.GET("/email/verify/:token", c.VerifyEmail).Name = routeNameVerifyEmail
func (h *Auth) Routes(g *echo.Group) {
g.GET("/logout", h.Logout, middleware.RequireAuthentication()).Name = routeNameLogout
g.GET("/email/verify/:token", h.VerifyEmail).Name = routeNameVerifyEmail
noAuth := g.Group("/user", middleware.RequireNoAuthentication())
noAuth.GET("/login", c.LoginPage).Name = routeNameLogin
noAuth.POST("/login", c.LoginSubmit).Name = routeNameLoginSubmit
noAuth.GET("/register", c.RegisterPage).Name = routeNameRegister
noAuth.POST("/register", c.RegisterSubmit).Name = routeNameRegisterSubmit
noAuth.GET("/password", c.ForgotPasswordPage).Name = routeNameForgotPassword
noAuth.POST("/password", c.ForgotPasswordSubmit).Name = routeNameForgotPasswordSubmit
noAuth.GET("/login", h.LoginPage).Name = routeNameLogin
noAuth.POST("/login", h.LoginSubmit).Name = routeNameLoginSubmit
noAuth.GET("/register", h.RegisterPage).Name = routeNameRegister
noAuth.POST("/register", h.RegisterSubmit).Name = routeNameRegisterSubmit
noAuth.GET("/password", h.ForgotPasswordPage).Name = routeNameForgotPassword
noAuth.POST("/password", h.ForgotPasswordSubmit).Name = routeNameForgotPasswordSubmit
resetGroup := noAuth.Group("/password/reset",
middleware.LoadUser(c.orm),
middleware.LoadValidPasswordToken(c.auth),
middleware.LoadUser(h.orm),
middleware.LoadValidPasswordToken(h.auth),
)
resetGroup.GET("/token/:user/:password_token/:token", c.ResetPasswordPage).Name = routeNameResetPassword
resetGroup.POST("/token/:user/:password_token/:token", c.ResetPasswordSubmit).Name = routeNameResetPasswordSubmit
resetGroup.GET("/token/:user/:password_token/:token", h.ResetPasswordPage).Name = routeNameResetPassword
resetGroup.POST("/token/:user/:password_token/:token", h.ResetPasswordSubmit).Name = routeNameResetPasswordSubmit
}
func (c *Auth) ForgotPasswordPage(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = templates.LayoutAuth
page.Name = templates.PageForgotPassword
page.Title = "Forgot password"
page.Form = form.Get[forgotPasswordForm](ctx)
func (h *Auth) ForgotPasswordPage(ctx echo.Context) error {
p := page.New(ctx)
p.Layout = templates.LayoutAuth
p.Name = templates.PageForgotPassword
p.Title = "Forgot password"
p.Form = form.Get[forgotPasswordForm](ctx)
return c.RenderPage(ctx, page)
return h.RenderPage(ctx, p)
}
func (c *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
func (h *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
var input forgotPasswordForm
succeed := func() error {
form.Clear(ctx)
msg.Success(ctx, "An email containing a link to reset your password will be sent to this address if it exists in our system.")
return c.ForgotPasswordPage(ctx)
return h.ForgotPasswordPage(ctx)
}
err := form.Submit(ctx, &input)
@ -121,13 +121,13 @@ func (c *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
switch err.(type) {
case nil:
case validator.ValidationErrors:
return c.ForgotPasswordPage(ctx)
return h.ForgotPasswordPage(ctx)
default:
return err
}
// Attempt to load the user
u, err := c.orm.User.
u, err := h.orm.User.
Query().
Where(user.Email(strings.ToLower(input.Email))).
Only(ctx.Request().Context())
@ -137,13 +137,13 @@ func (c *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
return succeed()
case nil:
default:
return c.Fail(err, "error querying user during forgot password")
return fail(err, "error querying user during forgot password")
}
// Generate the token
token, pt, err := c.auth.GeneratePasswordResetToken(ctx, u.ID)
token, pt, err := h.auth.GeneratePasswordResetToken(ctx, u.ID)
if err != nil {
return c.Fail(err, "error generating password reset token")
return fail(err, "error generating password reset token")
}
log.Ctx(ctx).Info("generated password reset token",
@ -152,7 +152,7 @@ func (c *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
// Email the user
url := ctx.Echo().Reverse(routeNameResetPassword, u.ID, pt.ID, token)
err = c.mail.
err = h.mail.
Compose().
To(u.Email).
Subject("Reset your password").
@ -160,30 +160,30 @@ func (c *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
Send(ctx)
if err != nil {
return c.Fail(err, "error sending password reset email")
return fail(err, "error sending password reset email")
}
return succeed()
}
func (c *Auth) LoginPage(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = templates.LayoutAuth
page.Name = templates.PageLogin
page.Title = "Log in"
page.Form = form.Get[loginForm](ctx)
func (h *Auth) LoginPage(ctx echo.Context) error {
p := page.New(ctx)
p.Layout = templates.LayoutAuth
p.Name = templates.PageLogin
p.Title = "Log in"
p.Form = form.Get[loginForm](ctx)
return c.RenderPage(ctx, page)
return h.RenderPage(ctx, p)
}
func (c *Auth) LoginSubmit(ctx echo.Context) error {
func (h *Auth) LoginSubmit(ctx echo.Context) error {
var input loginForm
authFailed := func() error {
input.SetFieldError("Email", "")
input.SetFieldError("Password", "")
msg.Danger(ctx, "Invalid credentials. Please try again.")
return c.LoginPage(ctx)
return h.LoginPage(ctx)
}
err := form.Submit(ctx, &input)
@ -191,13 +191,13 @@ func (c *Auth) LoginSubmit(ctx echo.Context) error {
switch err.(type) {
case nil:
case validator.ValidationErrors:
return c.LoginPage(ctx)
return h.LoginPage(ctx)
default:
return err
}
// Attempt to load the user
u, err := c.orm.User.
u, err := h.orm.User.
Query().
Where(user.Email(strings.ToLower(input.Email))).
Only(ctx.Request().Context())
@ -207,45 +207,45 @@ func (c *Auth) LoginSubmit(ctx echo.Context) error {
return authFailed()
case nil:
default:
return c.Fail(err, "error querying user during login")
return fail(err, "error querying user during login")
}
// Check if the password is correct
err = c.auth.CheckPassword(input.Password, u.Password)
err = h.auth.CheckPassword(input.Password, u.Password)
if err != nil {
return authFailed()
}
// Log the user in
err = c.auth.Login(ctx, u.ID)
err = h.auth.Login(ctx, u.ID)
if err != nil {
return c.Fail(err, "unable to log in user")
return fail(err, "unable to log in user")
}
msg.Success(ctx, fmt.Sprintf("Welcome back, <strong>%s</strong>. You are now logged in.", u.Name))
return c.Redirect(ctx, routeNameHome)
return redirect(ctx, routeNameHome)
}
func (c *Auth) Logout(ctx echo.Context) error {
if err := c.auth.Logout(ctx); err == nil {
func (h *Auth) Logout(ctx echo.Context) error {
if err := h.auth.Logout(ctx); err == nil {
msg.Success(ctx, "You have been logged out successfully.")
} else {
msg.Danger(ctx, "An error occurred. Please try again.")
}
return c.Redirect(ctx, routeNameHome)
return redirect(ctx, routeNameHome)
}
func (c *Auth) RegisterPage(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = templates.LayoutAuth
page.Name = templates.PageRegister
page.Title = "Register"
page.Form = form.Get[registerForm](ctx)
func (h *Auth) RegisterPage(ctx echo.Context) error {
p := page.New(ctx)
p.Layout = templates.LayoutAuth
p.Name = templates.PageRegister
p.Title = "Register"
p.Form = form.Get[registerForm](ctx)
return c.RenderPage(ctx, page)
return h.RenderPage(ctx, p)
}
func (c *Auth) RegisterSubmit(ctx echo.Context) error {
func (h *Auth) RegisterSubmit(ctx echo.Context) error {
var input registerForm
err := form.Submit(ctx, &input)
@ -253,19 +253,19 @@ func (c *Auth) RegisterSubmit(ctx echo.Context) error {
switch err.(type) {
case nil:
case validator.ValidationErrors:
return c.RegisterPage(ctx)
return h.RegisterPage(ctx)
default:
return err
}
// Hash the password
pwHash, err := c.auth.HashPassword(input.Password)
pwHash, err := h.auth.HashPassword(input.Password)
if err != nil {
return c.Fail(err, "unable to hash password")
return fail(err, "unable to hash password")
}
// Attempt creating the user
u, err := c.orm.User.
u, err := h.orm.User.
Create().
SetName(input.Name).
SetEmail(input.Email).
@ -280,33 +280,33 @@ func (c *Auth) RegisterSubmit(ctx echo.Context) error {
)
case *ent.ConstraintError:
msg.Warning(ctx, "A user with this email address already exists. Please log in.")
return c.Redirect(ctx, routeNameLogin)
return redirect(ctx, routeNameLogin)
default:
return c.Fail(err, "unable to create user")
return fail(err, "unable to create user")
}
// Log the user in
err = c.auth.Login(ctx, u.ID)
err = h.auth.Login(ctx, u.ID)
if err != nil {
log.Ctx(ctx).Error("unable to log user in",
"error", err,
"user_id", u.ID,
)
msg.Info(ctx, "Your account has been created.")
return c.Redirect(ctx, routeNameLogin)
return redirect(ctx, routeNameLogin)
}
msg.Success(ctx, "Your account has been created. You are now logged in.")
// Send the verification email
c.sendVerificationEmail(ctx, u)
h.sendVerificationEmail(ctx, u)
return c.Redirect(ctx, routeNameHome)
return redirect(ctx, routeNameHome)
}
func (c *Auth) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
func (h *Auth) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
// Generate a token
token, err := c.auth.GenerateEmailVerificationToken(usr.Email)
token, err := h.auth.GenerateEmailVerificationToken(usr.Email)
if err != nil {
log.Ctx(ctx).Error("unable to generate email verification token",
"user_id", usr.ID,
@ -317,7 +317,7 @@ func (c *Auth) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
// Send the email
url := ctx.Echo().Reverse(routeNameVerifyEmail, token)
err = c.mail.
err = h.mail.
Compose().
To(usr.Email).
Subject("Confirm your email address").
@ -335,17 +335,17 @@ func (c *Auth) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
msg.Info(ctx, "An email was sent to you to verify your email address.")
}
func (c *Auth) ResetPasswordPage(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = templates.LayoutAuth
page.Name = templates.PageResetPassword
page.Title = "Reset password"
page.Form = form.Get[resetPasswordForm](ctx)
func (h *Auth) ResetPasswordPage(ctx echo.Context) error {
p := page.New(ctx)
p.Layout = templates.LayoutAuth
p.Name = templates.PageResetPassword
p.Title = "Reset password"
p.Form = form.Get[resetPasswordForm](ctx)
return c.RenderPage(ctx, page)
return h.RenderPage(ctx, p)
}
func (c *Auth) ResetPasswordSubmit(ctx echo.Context) error {
func (h *Auth) ResetPasswordSubmit(ctx echo.Context) error {
var input resetPasswordForm
err := form.Submit(ctx, &input)
@ -353,15 +353,15 @@ func (c *Auth) ResetPasswordSubmit(ctx echo.Context) error {
switch err.(type) {
case nil:
case validator.ValidationErrors:
return c.ResetPasswordPage(ctx)
return h.ResetPasswordPage(ctx)
default:
return err
}
// Hash the new password
hash, err := c.auth.HashPassword(input.Password)
hash, err := h.auth.HashPassword(input.Password)
if err != nil {
return c.Fail(err, "unable to hash password")
return fail(err, "unable to hash password")
}
// Get the requesting user
@ -374,28 +374,28 @@ func (c *Auth) ResetPasswordSubmit(ctx echo.Context) error {
Save(ctx.Request().Context())
if err != nil {
return c.Fail(err, "unable to update password")
return fail(err, "unable to update password")
}
// Delete all password tokens for this user
err = c.auth.DeletePasswordTokens(ctx, usr.ID)
err = h.auth.DeletePasswordTokens(ctx, usr.ID)
if err != nil {
return c.Fail(err, "unable to delete password tokens")
return fail(err, "unable to delete password tokens")
}
msg.Success(ctx, "Your password has been updated.")
return c.Redirect(ctx, routeNameLogin)
return redirect(ctx, routeNameLogin)
}
func (c *Auth) VerifyEmail(ctx echo.Context) error {
func (h *Auth) VerifyEmail(ctx echo.Context) error {
var usr *ent.User
// Validate the token
token := ctx.Param("token")
email, err := c.auth.ValidateEmailVerificationToken(token)
email, err := h.auth.ValidateEmailVerificationToken(token)
if err != nil {
msg.Warning(ctx, "The link is either invalid or has expired.")
return c.Redirect(ctx, routeNameHome)
return redirect(ctx, routeNameHome)
}
// Check if it matches the authenticated user
@ -409,13 +409,13 @@ func (c *Auth) VerifyEmail(ctx echo.Context) error {
// Query to find a matching user, if needed
if usr == nil {
usr, err = c.orm.User.
usr, err = h.orm.User.
Query().
Where(user.Email(email)).
Only(ctx.Request().Context())
if err != nil {
return c.Fail(err, "query failed loading email verification token user")
return fail(err, "query failed loading email verification token user")
}
}
@ -427,10 +427,10 @@ func (c *Auth) VerifyEmail(ctx echo.Context) error {
Save(ctx.Request().Context())
if err != nil {
return c.Fail(err, "failed to set user as verified")
return fail(err, "failed to set user as verified")
}
}
msg.Success(ctx, "Your email has been successfully verified.")
return c.Redirect(ctx, routeNameHome)
return redirect(ctx, routeNameHome)
}

View file

@ -5,8 +5,8 @@ import (
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/form"
"github.com/mikestefanello/pagoda/pkg/page"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/templates"
)
@ -19,7 +19,7 @@ const (
type (
Contact struct {
mail *services.MailClient
controller.Controller
*services.TemplateRenderer
}
contactForm struct {
@ -34,28 +34,28 @@ func init() {
Register(new(Contact))
}
func (c *Contact) Init(ct *services.Container) error {
c.Controller = controller.NewController(ct)
c.mail = ct.Mail
func (h *Contact) Init(c *services.Container) error {
h.TemplateRenderer = c.TemplateRenderer
h.mail = c.Mail
return nil
}
func (c *Contact) Routes(g *echo.Group) {
g.GET("/contact", c.Page).Name = routeNameContact
g.POST("/contact", c.Submit).Name = routeNameContactSubmit
func (h *Contact) Routes(g *echo.Group) {
g.GET("/contact", h.Page).Name = routeNameContact
g.POST("/contact", h.Submit).Name = routeNameContactSubmit
}
func (c *Contact) Page(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = templates.LayoutMain
page.Name = templates.PageContact
page.Title = "Contact us"
page.Form = form.Get[contactForm](ctx)
func (h *Contact) Page(ctx echo.Context) error {
p := page.New(ctx)
p.Layout = templates.LayoutMain
p.Name = templates.PageContact
p.Title = "Contact us"
p.Form = form.Get[contactForm](ctx)
return c.RenderPage(ctx, page)
return h.RenderPage(ctx, p)
}
func (c *Contact) Submit(ctx echo.Context) error {
func (h *Contact) Submit(ctx echo.Context) error {
var input contactForm
err := form.Submit(ctx, &input)
@ -63,12 +63,12 @@ func (c *Contact) Submit(ctx echo.Context) error {
switch err.(type) {
case nil:
case validator.ValidationErrors:
return c.Page(ctx)
return h.Page(ctx)
default:
return err
}
err = c.mail.
err = h.mail.
Compose().
To(input.Email).
Subject("Contact form submitted").
@ -76,8 +76,8 @@ func (c *Contact) Submit(ctx echo.Context) error {
Send(ctx)
if err != nil {
return c.Fail(err, "unable to send email")
return fail(err, "unable to send email")
}
return c.Page(ctx)
return h.Page(ctx)
}

View file

@ -5,13 +5,14 @@ import (
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/context"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/log"
"github.com/mikestefanello/pagoda/pkg/page"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/templates"
)
type Error struct {
controller.Controller
*services.TemplateRenderer
}
func (e *Error) Page(err error, ctx echo.Context) {
@ -26,19 +27,23 @@ func (e *Error) Page(err error, ctx echo.Context) {
}
// Log the error
if code >= 500 {
log.Ctx(ctx).Error(err.Error())
logger := log.Ctx(ctx)
switch {
case code >= 500:
logger.Error(err.Error())
case code >= 400:
logger.Warn(err.Error())
}
// Render the error page
page := controller.NewPage(ctx)
page.Layout = templates.LayoutMain
page.Name = templates.PageError
page.Title = http.StatusText(code)
page.StatusCode = code
page.HTMX.Request.Enabled = false
p := page.New(ctx)
p.Layout = templates.LayoutMain
p.Name = templates.PageError
p.Title = http.StatusText(code)
p.StatusCode = code
p.HTMX.Request.Enabled = false
if err = e.RenderPage(ctx, page); err != nil {
if err = e.RenderPage(ctx, p); err != nil {
log.Ctx(ctx).Error("failed to render error page",
"error", err,
)

View file

@ -1,7 +1,11 @@
package handlers
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/htmx"
"github.com/mikestefanello/pagoda/pkg/services"
)
@ -25,3 +29,24 @@ func Register(h Handler) {
func GetHandlers() []Handler {
return handlers
}
// redirect redirects to a given route name with optional route parameters
func redirect(ctx echo.Context, route string, routeParams ...any) error {
url := ctx.Echo().Reverse(route, routeParams...)
if htmx.GetRequest(ctx).Boosted {
htmx.Response{
Redirect: url,
}.Apply(ctx)
return nil
} else {
return ctx.Redirect(http.StatusFound, url)
}
}
// fail is a helper to fail a request by returning a 500 error and logging the error
func fail(err error, log string) error {
// The error handler will handle logging
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("%s: %v", log, err))
}

View file

@ -0,0 +1,53 @@
package handlers
import (
"errors"
"net/http"
"testing"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/htmx"
"github.com/mikestefanello/pagoda/pkg/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetSetHandlers(t *testing.T) {
handlers = []Handler{}
assert.Empty(t, GetHandlers())
h := new(Pages)
Register(h)
got := GetHandlers()
require.Len(t, got, 1)
assert.Equal(t, h, got[0])
}
func TestRedirect(t *testing.T) {
c.Web.GET("/path/:first/and/:second", func(c echo.Context) error {
return nil
}).Name = "redirect-test"
t.Run("normal", func(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/abc")
err := redirect(ctx, "redirect-test", "one", "two")
require.NoError(t, err)
assert.Equal(t, "/path/one/and/two", ctx.Response().Header().Get(echo.HeaderLocation))
assert.Equal(t, http.StatusFound, ctx.Response().Status)
})
t.Run("htmx boosted", func(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/abc")
ctx.Request().Header.Set(htmx.HeaderBoosted, "true")
err := redirect(ctx, "redirect-test", "one", "two")
require.NoError(t, err)
assert.Equal(t, "/path/one/and/two", ctx.Response().Header().Get(htmx.HeaderRedirect))
})
}
func TestFail(t *testing.T) {
err := fail(errors.New("err message"), "log message")
require.IsType(t, new(echo.HTTPError), err)
he := err.(*echo.HTTPError)
assert.Equal(t, http.StatusInternalServerError, he.Code)
assert.Equal(t, "log message: err message", he.Message)
}

View file

@ -5,7 +5,7 @@ import (
"html/template"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/page"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/templates"
)
@ -17,7 +17,7 @@ const (
type (
Pages struct {
controller.Controller
*services.TemplateRenderer
}
post struct {
@ -41,30 +41,30 @@ func init() {
Register(new(Pages))
}
func (c *Pages) Init(ct *services.Container) error {
c.Controller = controller.NewController(ct)
func (h *Pages) Init(c *services.Container) error {
h.TemplateRenderer = c.TemplateRenderer
return nil
}
func (c *Pages) Routes(g *echo.Group) {
g.GET("/", c.Home).Name = routeNameHome
g.GET("/about", c.About).Name = routeNameAbout
func (h *Pages) Routes(g *echo.Group) {
g.GET("/", h.Home).Name = routeNameHome
g.GET("/about", h.About).Name = routeNameAbout
}
func (c *Pages) Home(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = templates.LayoutMain
page.Name = templates.PageHome
page.Metatags.Description = "Welcome to the homepage."
page.Metatags.Keywords = []string{"Go", "MVC", "Web", "Software"}
page.Pager = controller.NewPager(ctx, 4)
page.Data = c.fetchPosts(&page.Pager)
func (h *Pages) Home(ctx echo.Context) error {
p := page.New(ctx)
p.Layout = templates.LayoutMain
p.Name = templates.PageHome
p.Metatags.Description = "Welcome to the homepage."
p.Metatags.Keywords = []string{"Go", "MVC", "Web", "Software"}
p.Pager = page.NewPager(ctx, 4)
p.Data = h.fetchPosts(&p.Pager)
return c.RenderPage(ctx, page)
return h.RenderPage(ctx, p)
}
// fetchPosts is an mock example of fetching posts to illustrate how paging works
func (c *Pages) fetchPosts(pager *controller.Pager) []post {
func (h *Pages) fetchPosts(pager *page.Pager) []post {
pager.SetItems(20)
posts := make([]post, 20)
@ -77,19 +77,19 @@ func (c *Pages) fetchPosts(pager *controller.Pager) []post {
return posts[pager.GetOffset() : pager.GetOffset()+pager.ItemsPerPage]
}
func (c *Pages) About(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = templates.LayoutMain
page.Name = templates.PageAbout
page.Title = "About"
func (h *Pages) About(ctx echo.Context) error {
p := page.New(ctx)
p.Layout = templates.LayoutMain
p.Name = templates.PageAbout
p.Title = "About"
// This page will be cached!
page.Cache.Enabled = true
page.Cache.Tags = []string{"page_about", "page:list"}
p.Cache.Enabled = true
p.Cache.Tags = []string{"page_about", "page:list"}
// A simple example of how the Data field can contain anything you want to send to the templates
// even though you wouldn't normally send markup like this
page.Data = aboutData{
p.Data = aboutData{
ShowCacheWarning: true,
FrontendTabs: []aboutTab{
{
@ -117,5 +117,5 @@ func (c *Pages) About(ctx echo.Context) error {
},
}
return c.RenderPage(ctx, page)
return h.RenderPage(ctx, p)
}

View file

@ -7,7 +7,6 @@ import (
"github.com/labstack/echo-contrib/session"
echomw "github.com/labstack/echo/v4/middleware"
"github.com/mikestefanello/pagoda/config"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/middleware"
"github.com/mikestefanello/pagoda/pkg/services"
)
@ -43,14 +42,14 @@ func BuildRouter(c *services.Container) error {
}),
session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
middleware.LoadAuthenticatedUser(c.Auth),
middleware.ServeCachedPage(c.Cache),
middleware.ServeCachedPage(c.TemplateRenderer),
echomw.CSRFWithConfig(echomw.CSRFConfig{
TokenLookup: "form:csrf",
}),
)
// Error handler
err := Error{Controller: controller.NewController(c)}
err := Error{c.TemplateRenderer}
c.Web.HTTPErrorHandler = err.Page
// Initialize and register all handlers

View file

@ -5,7 +5,7 @@ import (
"math/rand"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/controller"
"github.com/mikestefanello/pagoda/pkg/page"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/templates"
)
@ -14,7 +14,7 @@ const routeNameSearch = "search"
type (
Search struct {
controller.Controller
*services.TemplateRenderer
}
searchResult struct {
@ -27,19 +27,19 @@ func init() {
Register(new(Search))
}
func (c *Search) Init(ct *services.Container) error {
c.Controller = controller.NewController(ct)
func (h *Search) Init(c *services.Container) error {
h.TemplateRenderer = c.TemplateRenderer
return nil
}
func (c *Search) Routes(g *echo.Group) {
g.GET("/search", c.Page).Name = routeNameSearch
func (h *Search) Routes(g *echo.Group) {
g.GET("/search", h.Page).Name = routeNameSearch
}
func (c *Search) Page(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = templates.LayoutMain
page.Name = templates.PageSearch
func (h *Search) Page(ctx echo.Context) error {
p := page.New(ctx)
p.Layout = templates.LayoutMain
p.Name = templates.PageSearch
// Fake search results
var results []searchResult
@ -54,7 +54,7 @@ func (c *Search) Page(ctx echo.Context) error {
})
}
}
page.Data = results
p.Data = results
return c.RenderPage(ctx, page)
return h.RenderPage(ctx, p)
}