saasitone/routes/router.go

112 lines
3.1 KiB
Go
Raw Normal View History

2021-12-14 08:13:53 -08:00
package routes
2021-12-03 03:11:01 -08:00
import (
"net/http"
2021-12-10 05:33:49 -08:00
"goweb/config"
"goweb/controller"
2021-12-03 05:18:40 -08:00
"goweb/middleware"
"github.com/go-playground/validator/v10"
2021-12-03 03:11:01 -08:00
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
2021-12-07 18:36:57 -08:00
2021-12-03 03:11:01 -08:00
"github.com/labstack/echo/v4"
2021-12-03 05:18:40 -08:00
echomw "github.com/labstack/echo/v4/middleware"
2021-12-03 03:11:01 -08:00
"goweb/container"
)
type Validator struct {
validator *validator.Validate
}
func (v *Validator) Validate(i interface{}) error {
if err := v.validator.Struct(i); err != nil {
return err
}
return nil
}
// TODO: This is doing more than building the router
2021-12-03 03:11:01 -08:00
func BuildRouter(c *container.Container) {
2021-12-07 18:36:57 -08:00
// Static files with proper cache control
// funcmap.File() should be used in templates to append a cache key to the URL in order to break cache
// after each server restart
c.Web.Group("", middleware.CacheControl(c.Config.Cache.Expiration.StaticFile)).
2021-12-10 05:33:49 -08:00
Static(config.StaticPrefix, config.StaticDir)
2021-12-07 18:36:57 -08:00
2021-12-03 03:11:01 -08:00
// Middleware
g := c.Web.Group("",
echomw.RemoveTrailingSlashWithConfig(echomw.TrailingSlashConfig{
RedirectCode: http.StatusMovedPermanently,
}),
echomw.Recover(),
2021-12-08 19:21:07 -08:00
echomw.Secure(),
echomw.RequestID(),
echomw.Gzip(),
echomw.Logger(),
middleware.LogRequestID(),
echomw.TimeoutWithConfig(echomw.TimeoutConfig{
Timeout: c.Config.App.Timeout,
}),
middleware.PageCache(c.Cache),
session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
echomw.CSRFWithConfig(echomw.CSRFConfig{
TokenLookup: "form:csrf",
}),
2021-12-15 06:29:43 -08:00
middleware.LoadAuthenticatedUser(c.Auth),
)
2021-12-03 05:06:06 -08:00
2021-12-03 03:11:01 -08:00
// Base controller
ctr := controller.NewController(c)
2021-12-03 03:11:01 -08:00
// Error handler
2021-12-05 17:22:45 -08:00
err := Error{Controller: ctr}
2021-12-03 03:11:01 -08:00
c.Web.HTTPErrorHandler = err.Get
// Validator
c.Web.Validator = &Validator{validator: validator.New()}
2021-12-03 03:11:01 -08:00
// Routes
navRoutes(c, g, ctr)
userRoutes(c, g, ctr)
2021-12-03 03:11:01 -08:00
}
func navRoutes(c *container.Container, g *echo.Group, ctr controller.Controller) {
2021-12-05 17:22:45 -08:00
home := Home{Controller: ctr}
g.GET("/", home.Get).Name = "home"
2021-12-03 03:11:01 -08:00
2021-12-05 17:22:45 -08:00
about := About{Controller: ctr}
g.GET("/about", about.Get).Name = "about"
2021-12-03 03:11:01 -08:00
2021-12-05 17:22:45 -08:00
contact := Contact{Controller: ctr}
g.GET("/contact", contact.Get).Name = "contact"
g.POST("/contact", contact.Post).Name = "contact.post"
2021-12-03 03:11:01 -08:00
}
func userRoutes(c *container.Container, g *echo.Group, ctr controller.Controller) {
2021-12-11 20:17:12 -08:00
logout := Logout{Controller: ctr}
2021-12-12 14:04:11 -08:00
g.GET("/logout", logout.Get, middleware.RequireAuthentication()).Name = "logout"
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"
2021-12-11 20:17:12 -08:00
2021-12-05 17:22:45 -08:00
register := Register{Controller: ctr}
2021-12-12 14:04:11 -08:00
noAuth.GET("/register", register.Get).Name = "register"
noAuth.POST("/register", register.Post).Name = "register.post"
2021-12-14 18:59:56 -08:00
forgot := ForgotPassword{Controller: ctr}
noAuth.GET("/password", forgot.Get).Name = "forgot_password"
noAuth.POST("/password", forgot.Post).Name = "forgot_password.post"
resetGroup := noAuth.Group("/password/reset", middleware.LoadValidPasswordToken(c.Auth))
reset := ResetPassword{Controller: ctr}
resetGroup.GET("/token/:password_token", reset.Get).Name = "reset_password"
resetGroup.POST("/token/:password_token", reset.Post).Name = "reset_password.post"
2021-12-03 03:11:01 -08:00
}