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" "goweb/container"
) )
const StaticDir = "static" const (
StaticDir = "static"
StaticPrefix = "files"
)
func BuildRouter(c *container.Container) { func BuildRouter(c *container.Container) {
// Static files with proper cache control // 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 // funcmap.File() should be used in templates to append a cache key to the URL in order to break cache
// after each server restart // after each server restart
c.Web.Group("", middleware.CacheControl(c.Config.Cache.MaxAge.StaticFile)). c.Web.Group("", middleware.CacheControl(c.Config.Cache.MaxAge.StaticFile)).
Static("/", StaticDir) Static(StaticPrefix, StaticDir)
// Middleware // Middleware
c.Web.Use(echomw.RemoveTrailingSlashWithConfig(echomw.TrailingSlashConfig{ g := c.Web.Group("",
RedirectCode: http.StatusMovedPermanently, echomw.RemoveTrailingSlashWithConfig(echomw.TrailingSlashConfig{
})) RedirectCode: http.StatusMovedPermanently,
c.Web.Use(echomw.RequestID()) }),
c.Web.Use(echomw.Recover()) echomw.RequestID(),
c.Web.Use(echomw.Gzip()) echomw.Recover(),
c.Web.Use(echomw.Logger()) echomw.Gzip(),
c.Web.Use(echomw.TimeoutWithConfig(echomw.TimeoutConfig{ echomw.Logger(),
Timeout: c.Config.App.Timeout, 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)))) middleware.PageCache(c.Cache),
c.Web.Use(echomw.CSRFWithConfig(echomw.CSRFConfig{ session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
TokenLookup: "form:csrf", echomw.CSRFWithConfig(echomw.CSRFConfig{
})) TokenLookup: "form:csrf",
}),
)
// Base controller // Base controller
ctr := NewController(c) ctr := NewController(c)
@ -48,28 +53,28 @@ func BuildRouter(c *container.Container) {
c.Web.HTTPErrorHandler = err.Get c.Web.HTTPErrorHandler = err.Get
// Routes // Routes
navRoutes(c.Web, ctr) navRoutes(g, ctr)
userRoutes(c.Web, ctr) userRoutes(g, ctr)
} }
func navRoutes(e *echo.Echo, ctr Controller) { func navRoutes(g *echo.Group, ctr Controller) {
home := Home{Controller: ctr} home := Home{Controller: ctr}
e.GET("/", home.Get).Name = "home" g.GET("/", home.Get).Name = "home"
about := About{Controller: ctr} about := About{Controller: ctr}
e.GET("/about", about.Get).Name = "about" g.GET("/about", about.Get).Name = "about"
contact := Contact{Controller: ctr} contact := Contact{Controller: ctr}
e.GET("/contact", contact.Get).Name = "contact" g.GET("/contact", contact.Get).Name = "contact"
e.POST("/contact", contact.Post).Name = "contact.post" 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} login := Login{Controller: ctr}
e.GET("/user/login", login.Get).Name = "login" g.GET("/user/login", login.Get).Name = "login"
e.POST("/user/login", login.Post).Name = "login.post" g.POST("/user/login", login.Post).Name = "login.post"
register := Register{Controller: ctr} register := Register{Controller: ctr}
e.GET("/user/register", register.Get).Name = "register" g.GET("/user/register", register.Get).Name = "register"
e.POST("/user/register", register.Post).Name = "register.post" 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 // File appends a cache key to a given filepath so it can remain cached until the app is restarted
func File(filepath string) string { 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 { 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) 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 { if page.Headers != nil {
for k, v := range page.Headers { for k, v := range page.Headers {