Group middleware between static and non-static file routes.

This commit is contained in:
mikestefanello 2021-12-08 18:58:55 -05:00
parent 30dced6315
commit 4096691df0
3 changed files with 40 additions and 31 deletions

View File

@ -14,31 +14,36 @@ import (
"goweb/container"
)
const StaticDir = "static"
const (
StaticDir = "static"
StaticPrefix = "files"
)
func BuildRouter(c *container.Container) {
// 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.MaxAge.StaticFile)).
Static("/", StaticDir)
Static(StaticPrefix, StaticDir)
// Middleware
c.Web.Use(echomw.RemoveTrailingSlashWithConfig(echomw.TrailingSlashConfig{
g := c.Web.Group("",
echomw.RemoveTrailingSlashWithConfig(echomw.TrailingSlashConfig{
RedirectCode: http.StatusMovedPermanently,
}))
c.Web.Use(echomw.RequestID())
c.Web.Use(echomw.Recover())
c.Web.Use(echomw.Gzip())
c.Web.Use(echomw.Logger())
c.Web.Use(echomw.TimeoutWithConfig(echomw.TimeoutConfig{
}),
echomw.RequestID(),
echomw.Recover(),
echomw.Gzip(),
echomw.Logger(),
echomw.TimeoutWithConfig(echomw.TimeoutConfig{
Timeout: c.Config.App.Timeout,
}))
c.Web.Use(middleware.PageCache(c.Cache))
c.Web.Use(session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))))
c.Web.Use(echomw.CSRFWithConfig(echomw.CSRFConfig{
}),
middleware.PageCache(c.Cache),
session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
echomw.CSRFWithConfig(echomw.CSRFConfig{
TokenLookup: "form:csrf",
}))
}),
)
// Base controller
ctr := NewController(c)
@ -48,28 +53,28 @@ func BuildRouter(c *container.Container) {
c.Web.HTTPErrorHandler = err.Get
// Routes
navRoutes(c.Web, ctr)
userRoutes(c.Web, ctr)
navRoutes(g, ctr)
userRoutes(g, ctr)
}
func navRoutes(e *echo.Echo, ctr Controller) {
func navRoutes(g *echo.Group, ctr Controller) {
home := Home{Controller: ctr}
e.GET("/", home.Get).Name = "home"
g.GET("/", home.Get).Name = "home"
about := About{Controller: ctr}
e.GET("/about", about.Get).Name = "about"
g.GET("/about", about.Get).Name = "about"
contact := Contact{Controller: ctr}
e.GET("/contact", contact.Get).Name = "contact"
e.POST("/contact", contact.Post).Name = "contact.post"
g.GET("/contact", contact.Get).Name = "contact"
g.POST("/contact", contact.Post).Name = "contact.post"
}
func userRoutes(e *echo.Echo, ctr Controller) {
func userRoutes(g *echo.Group, ctr Controller) {
login := Login{Controller: ctr}
e.GET("/user/login", login.Get).Name = "login"
e.POST("/user/login", login.Post).Name = "login.post"
g.GET("/user/login", login.Get).Name = "login"
g.POST("/user/login", login.Post).Name = "login.post"
register := Register{Controller: ctr}
e.GET("/user/register", register.Get).Name = "register"
e.POST("/user/register", register.Post).Name = "register.post"
g.GET("/user/register", register.Get).Name = "register"
g.POST("/user/register", register.Post).Name = "register.post"
}

View File

@ -45,7 +45,7 @@ func HasField(v interface{}, name string) bool {
// File appends a cache key to a given filepath so it can remain cached until the app is restarted
func File(filepath string) string {
return fmt.Sprintf("%s?v=%s", filepath, CacheKey)
return fmt.Sprintf("/files/%s?v=%s", filepath, CacheKey)
}
func Link(url, text, currentPath string, classes ...string) template.HTML {

View File

@ -32,7 +32,11 @@ func PageCache(ch *cache.Cache) echo.MiddlewareFunc {
return next(c)
}
page := res.(*CachedPage)
page, ok := res.(*CachedPage)
if !ok {
c.Logger().Errorf("failed casting cached page: %s", key)
return next(c)
}
if page.Headers != nil {
for k, v := range page.Headers {