Use consts for route names and templates.
This commit is contained in:
parent
a787d5dc7f
commit
60c8aefd49
30 changed files with 135 additions and 82 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"html/template"
|
||||
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -27,8 +28,8 @@ type (
|
|||
|
||||
func (c *about) Get(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "main"
|
||||
page.Name = "about"
|
||||
page.Layout = templates.LayoutMain
|
||||
page.Name = templates.PageAbout
|
||||
page.Title = "About"
|
||||
|
||||
// This page will be cached!
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import (
|
|||
// this test package
|
||||
func TestAbout_Get(t *testing.T) {
|
||||
doc := request(t).
|
||||
setRoute("about").
|
||||
setRoute(routeNameAbout).
|
||||
get().
|
||||
assertStatusCode(http.StatusOK).
|
||||
toDoc()
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -23,8 +24,8 @@ type (
|
|||
|
||||
func (c *contact) Get(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "main"
|
||||
page.Name = "contact"
|
||||
page.Layout = templates.LayoutMain
|
||||
page.Name = templates.PageContact
|
||||
page.Title = "Contact us"
|
||||
page.Form = contactForm{}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -30,9 +31,9 @@ func (e *errorHandler) Get(err error, ctx echo.Context) {
|
|||
}
|
||||
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "main"
|
||||
page.Title = http.StatusText(code)
|
||||
page.Name = "error"
|
||||
page.Layout = templates.LayoutMain
|
||||
page.Name = templates.PageError
|
||||
page.StatusCode = code
|
||||
page.HTMX.Request.Enabled = false
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/pkg/msg"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -26,8 +27,8 @@ type (
|
|||
|
||||
func (c *forgotPassword) Get(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "auth"
|
||||
page.Name = "forgot-password"
|
||||
page.Layout = templates.LayoutAuth
|
||||
page.Name = templates.PageForgotPassword
|
||||
page.Title = "Forgot password"
|
||||
page.Form = forgotPasswordForm{}
|
||||
|
||||
|
|
@ -84,7 +85,7 @@ func (c *forgotPassword) Post(ctx echo.Context) error {
|
|||
ctx.Logger().Infof("generated password reset token for user %d", u.ID)
|
||||
|
||||
// Email the user
|
||||
url := ctx.Echo().Reverse("reset_password", u.ID, pt.ID, token)
|
||||
url := ctx.Echo().Reverse(routeNameResetPassword, u.ID, pt.ID, token)
|
||||
err = c.Container.Mail.
|
||||
Compose().
|
||||
To(u.Email).
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -21,8 +22,8 @@ type (
|
|||
|
||||
func (c *home) Get(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "main"
|
||||
page.Name = "home"
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/pkg/msg"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -27,8 +28,8 @@ type (
|
|||
|
||||
func (c *login) Get(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "auth"
|
||||
page.Name = "login"
|
||||
page.Layout = templates.LayoutAuth
|
||||
page.Name = templates.PageLogin
|
||||
page.Title = "Log in"
|
||||
page.Form = loginForm{}
|
||||
|
||||
|
|
@ -90,5 +91,5 @@ func (c *login) Post(ctx echo.Context) error {
|
|||
}
|
||||
|
||||
msg.Success(ctx, fmt.Sprintf("Welcome back, <strong>%s</strong>. You are now logged in.", u.Name))
|
||||
return c.Redirect(ctx, "home")
|
||||
return c.Redirect(ctx, routeNameHome)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,5 +17,5 @@ func (l *logout) Get(c echo.Context) error {
|
|||
} else {
|
||||
msg.Danger(c, "An error occurred. Please try again.")
|
||||
}
|
||||
return l.Redirect(c, "home")
|
||||
return l.Redirect(c, routeNameHome)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/pkg/msg"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -27,8 +28,8 @@ type (
|
|||
|
||||
func (c *register) Get(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "auth"
|
||||
page.Name = "register"
|
||||
page.Layout = templates.LayoutAuth
|
||||
page.Name = templates.PageRegister
|
||||
page.Title = "Register"
|
||||
page.Form = registerForm{}
|
||||
|
||||
|
|
@ -75,7 +76,7 @@ func (c *register) Post(ctx echo.Context) error {
|
|||
ctx.Logger().Infof("user created: %s", u.Name)
|
||||
case *ent.ConstraintError:
|
||||
msg.Warning(ctx, "A user with this email address already exists. Please log in.")
|
||||
return c.Redirect(ctx, "login")
|
||||
return c.Redirect(ctx, routeNameLogin)
|
||||
default:
|
||||
return c.Fail(err, "unable to create user")
|
||||
}
|
||||
|
|
@ -85,7 +86,7 @@ func (c *register) Post(ctx echo.Context) error {
|
|||
if err != nil {
|
||||
ctx.Logger().Errorf("unable to log in: %v", err)
|
||||
msg.Info(ctx, "Your account has been created.")
|
||||
return c.Redirect(ctx, "login")
|
||||
return c.Redirect(ctx, routeNameLogin)
|
||||
}
|
||||
|
||||
msg.Success(ctx, "Your account has been created. You are now logged in.")
|
||||
|
|
@ -93,7 +94,7 @@ func (c *register) Post(ctx echo.Context) error {
|
|||
// Send the verification email
|
||||
c.sendVerificationEmail(ctx, u)
|
||||
|
||||
return c.Redirect(ctx, "home")
|
||||
return c.Redirect(ctx, routeNameHome)
|
||||
}
|
||||
|
||||
func (c *register) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
|
||||
|
|
@ -105,7 +106,7 @@ func (c *register) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
|
|||
}
|
||||
|
||||
// Send the email
|
||||
url := ctx.Echo().Reverse("verify_email", token)
|
||||
url := ctx.Echo().Reverse(routeNameVerifyEmail, token)
|
||||
err = c.Container.Mail.
|
||||
Compose().
|
||||
To(usr.Email).
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"github.com/mikestefanello/pagoda/pkg/context"
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/pkg/msg"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -23,8 +24,8 @@ type (
|
|||
|
||||
func (c *resetPassword) Get(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "auth"
|
||||
page.Name = "reset-password"
|
||||
page.Layout = templates.LayoutAuth
|
||||
page.Name = templates.PageResetPassword
|
||||
page.Title = "Reset password"
|
||||
page.Form = resetPasswordForm{}
|
||||
|
||||
|
|
@ -78,5 +79,5 @@ func (c *resetPassword) Post(ctx echo.Context) error {
|
|||
}
|
||||
|
||||
msg.Success(ctx, "Your password has been updated.")
|
||||
return c.Redirect(ctx, "login")
|
||||
return c.Redirect(ctx, routeNameLogin)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,24 @@ import (
|
|||
echomw "github.com/labstack/echo/v4/middleware"
|
||||
)
|
||||
|
||||
const (
|
||||
routeNameForgotPassword = "forgot_password"
|
||||
routeNameForgotPasswordSubmit = "forgot_password.submit"
|
||||
routeNameLogin = "login"
|
||||
routeNameLoginSubmit = "login.submit"
|
||||
routeNameLogout = "logout"
|
||||
routeNameRegister = "register"
|
||||
routeNameRegisterSubmit = "register.submit"
|
||||
routeNameResetPassword = "reset_password"
|
||||
routeNameResetPasswordSubmit = "reset_password.submit"
|
||||
routeNameVerifyEmail = "verify_email"
|
||||
routeNameContact = "contact"
|
||||
routeNameContactSubmit = "contact.submit"
|
||||
routeNameAbout = "about"
|
||||
routeNameHome = "home"
|
||||
routeNameSearch = "search"
|
||||
)
|
||||
|
||||
// BuildRouter builds the router
|
||||
func BuildRouter(c *services.Container) {
|
||||
// Static files with proper cache control
|
||||
|
|
@ -66,44 +84,44 @@ func BuildRouter(c *services.Container) {
|
|||
|
||||
func navRoutes(c *services.Container, g *echo.Group, ctr controller.Controller) {
|
||||
home := home{Controller: ctr}
|
||||
g.GET("/", home.Get).Name = "home"
|
||||
g.GET("/", home.Get).Name = routeNameHome
|
||||
|
||||
search := search{Controller: ctr}
|
||||
g.GET("/search", search.Get).Name = "search"
|
||||
g.GET("/search", search.Get).Name = routeNameSearch
|
||||
|
||||
about := about{Controller: ctr}
|
||||
g.GET("/about", about.Get).Name = "about"
|
||||
g.GET("/about", about.Get).Name = routeNameAbout
|
||||
|
||||
contact := contact{Controller: ctr}
|
||||
g.GET("/contact", contact.Get).Name = "contact"
|
||||
g.POST("/contact", contact.Post).Name = "contact.post"
|
||||
g.GET("/contact", contact.Get).Name = routeNameContact
|
||||
g.POST("/contact", contact.Post).Name = routeNameContactSubmit
|
||||
}
|
||||
|
||||
func userRoutes(c *services.Container, g *echo.Group, ctr controller.Controller) {
|
||||
logout := logout{Controller: ctr}
|
||||
g.GET("/logout", logout.Get, middleware.RequireAuthentication()).Name = "logout"
|
||||
g.GET("/logout", logout.Get, middleware.RequireAuthentication()).Name = routeNameLogout
|
||||
|
||||
verifyEmail := verifyEmail{Controller: ctr}
|
||||
g.GET("/email/verify/:token", verifyEmail.Get).Name = "verify_email"
|
||||
g.GET("/email/verify/:token", verifyEmail.Get).Name = routeNameVerifyEmail
|
||||
|
||||
noAuth := g.Group("/user", middleware.RequireNoAuthentication())
|
||||
login := login{Controller: ctr}
|
||||
noAuth.GET("/login", login.Get).Name = "login"
|
||||
noAuth.POST("/login", login.Post).Name = "login.post"
|
||||
noAuth.GET("/login", login.Get).Name = routeNameLogin
|
||||
noAuth.POST("/login", login.Post).Name = routeNameLoginSubmit
|
||||
|
||||
register := register{Controller: ctr}
|
||||
noAuth.GET("/register", register.Get).Name = "register"
|
||||
noAuth.POST("/register", register.Post).Name = "register.post"
|
||||
noAuth.GET("/register", register.Get).Name = routeNameRegister
|
||||
noAuth.POST("/register", register.Post).Name = routeNameRegisterSubmit
|
||||
|
||||
forgot := forgotPassword{Controller: ctr}
|
||||
noAuth.GET("/password", forgot.Get).Name = "forgot_password"
|
||||
noAuth.POST("/password", forgot.Post).Name = "forgot_password.post"
|
||||
noAuth.GET("/password", forgot.Get).Name = routeNameForgotPassword
|
||||
noAuth.POST("/password", forgot.Post).Name = routeNameForgotPasswordSubmit
|
||||
|
||||
resetGroup := noAuth.Group("/password/reset",
|
||||
middleware.LoadUser(c.ORM),
|
||||
middleware.LoadValidPasswordToken(c.Auth),
|
||||
)
|
||||
reset := resetPassword{Controller: ctr}
|
||||
resetGroup.GET("/token/:user/:password_token/:token", reset.Get).Name = "reset_password"
|
||||
resetGroup.POST("/token/:user/:password_token/:token", reset.Post).Name = "reset_password.post"
|
||||
resetGroup.GET("/token/:user/:password_token/:token", reset.Get).Name = routeNameResetPassword
|
||||
resetGroup.POST("/token/:user/:password_token/:token", reset.Post).Name = routeNameResetPasswordSubmit
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ func (h *httpRequest) setClient(client http.Client) *httpRequest {
|
|||
return h
|
||||
}
|
||||
|
||||
func (h *httpRequest) setRoute(route string, params ...interface{}) *httpRequest {
|
||||
func (h *httpRequest) setRoute(route string, params ...any) *httpRequest {
|
||||
h.route = srv.URL + c.Web.Reverse(route, params)
|
||||
return h
|
||||
}
|
||||
|
|
@ -122,7 +122,7 @@ func (h *httpResponse) assertStatusCode(code int) *httpResponse {
|
|||
return h
|
||||
}
|
||||
|
||||
func (h *httpResponse) assertRedirect(t *testing.T, route string, params ...interface{}) *httpResponse {
|
||||
func (h *httpResponse) assertRedirect(t *testing.T, route string, params ...any) *httpResponse {
|
||||
assert.Equal(t, c.Web.Reverse(route, params), h.Header.Get("Location"))
|
||||
return h
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"math/rand"
|
||||
|
||||
"github.com/mikestefanello/pagoda/pkg/controller"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -22,8 +23,8 @@ type (
|
|||
|
||||
func (c *search) Get(ctx echo.Context) error {
|
||||
page := controller.NewPage(ctx)
|
||||
page.Layout = "main"
|
||||
page.Name = "search"
|
||||
page.Layout = templates.LayoutMain
|
||||
page.Name = templates.PageSearch
|
||||
|
||||
// Fake search results
|
||||
var results []searchResult
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ func (c *verifyEmail) Get(ctx echo.Context) error {
|
|||
email, err := c.Container.Auth.ValidateEmailVerificationToken(token)
|
||||
if err != nil {
|
||||
msg.Warning(ctx, "The link is either invalid or has expired.")
|
||||
return c.Redirect(ctx, "home")
|
||||
return c.Redirect(ctx, routeNameHome)
|
||||
}
|
||||
|
||||
// Check if it matches the authenticated user
|
||||
|
|
@ -58,5 +58,5 @@ func (c *verifyEmail) Get(ctx echo.Context) error {
|
|||
}
|
||||
|
||||
msg.Success(ctx, "Your email has been successfully verified.")
|
||||
return c.Redirect(ctx, "home")
|
||||
return c.Redirect(ctx, routeNameHome)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue