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"
|
2021-12-12 19:09:13 -08:00
|
|
|
"goweb/controller"
|
2021-12-03 05:18:40 -08:00
|
|
|
"goweb/middleware"
|
2021-12-18 07:07:12 -08:00
|
|
|
"goweb/services"
|
2021-12-03 05:18:40 -08:00
|
|
|
|
2021-12-11 10:03:10 -08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-12-11 10:03:10 -08:00
|
|
|
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-18 07:07:12 -08:00
|
|
|
func BuildRouter(c *services.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
|
2021-12-11 10:41:57 -08:00
|
|
|
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
|
2021-12-08 15:58:55 -08:00
|
|
|
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(),
|
2021-12-08 15:58:55 -08:00
|
|
|
echomw.Gzip(),
|
|
|
|
echomw.Logger(),
|
2021-12-08 18:55:30 -08:00
|
|
|
middleware.LogRequestID(),
|
2021-12-08 15:58:55 -08:00
|
|
|
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-08 15:58:55 -08:00
|
|
|
)
|
2021-12-03 05:06:06 -08:00
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
// Base controller
|
2021-12-12 19:09:13 -08:00
|
|
|
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
|
|
|
|
|
2021-12-11 10:03:10 -08:00
|
|
|
// Validator
|
|
|
|
c.Web.Validator = &Validator{validator: validator.New()}
|
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
// Routes
|
2021-12-16 04:29:16 -08:00
|
|
|
navRoutes(c, g, ctr)
|
|
|
|
userRoutes(c, g, ctr)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2021-12-18 07:07:12 -08:00
|
|
|
func navRoutes(c *services.Container, g *echo.Group, ctr controller.Controller) {
|
2021-12-05 17:22:45 -08:00
|
|
|
home := Home{Controller: ctr}
|
2021-12-08 15:58:55 -08:00
|
|
|
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}
|
2021-12-08 15:58:55 -08:00
|
|
|
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}
|
2021-12-08 15:58:55 -08:00
|
|
|
g.GET("/contact", contact.Get).Name = "contact"
|
|
|
|
g.POST("/contact", contact.Post).Name = "contact.post"
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2021-12-18 07:07:12 -08:00
|
|
|
func userRoutes(c *services.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"
|
2021-12-16 04:29:16 -08:00
|
|
|
|
2021-12-16 18:27:52 -08:00
|
|
|
resetGroup := noAuth.Group("/password/reset",
|
|
|
|
middleware.LoadUser(c.ORM),
|
|
|
|
middleware.LoadValidPasswordToken(c.Auth),
|
|
|
|
)
|
2021-12-16 04:29:16 -08:00
|
|
|
reset := ResetPassword{Controller: ctr}
|
2021-12-16 16:49:33 -08:00
|
|
|
resetGroup.GET("/token/:user/:password_token", reset.Get).Name = "reset_password"
|
|
|
|
resetGroup.POST("/token/:user/:password_token", reset.Post).Name = "reset_password.post"
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|