saasitone/routes/router.go

104 lines
3.0 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"
2021-12-18 07:07:12 -08:00
"goweb/services"
2021-12-03 05:18:40 -08:00
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
)
// BuildRouter builds 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
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-20 09:33:14 -08:00
// Non static file route group
g := c.Web.Group("")
// Force HTTPS, if enabled
if c.Config.HTTP.TLS.Enabled {
g.Use(echomw.HTTPSRedirect())
}
g.Use(
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,
}),
session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
2021-12-20 09:33:14 -08:00
middleware.LoadAuthenticatedUser(c.Auth),
middleware.ServeCachedPage(c.Cache),
echomw.CSRFWithConfig(echomw.CSRFConfig{
TokenLookup: "form:csrf",
}),
)
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
// Example routes
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}
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
}
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"
resetGroup := noAuth.Group("/password/reset",
middleware.LoadUser(c.ORM),
middleware.LoadValidPasswordToken(c.Auth),
)
reset := ResetPassword{Controller: ctr}
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
}