From 855d67409f109f4853f11b74d5aec800e5c7ef1f Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Mon, 6 Dec 2021 21:51:21 -0500 Subject: [PATCH] Added timeout middleware and config. --- config/config.go | 9 +++++---- controllers/controller.go | 6 +++--- controllers/page.go | 4 ++-- controllers/router.go | 4 +++- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index f762b2d..a653e31 100644 --- a/config/config.go +++ b/config/config.go @@ -34,9 +34,10 @@ type HTTPConfig struct { // AppConfig stores application configuration type AppConfig struct { - Name string `env:"APP_NAME,default=Goweb"` - Environment Env `env:"APP_ENV,default=local"` - EncryptionKey string `env:"APP_ENCRYPTION_KEY,default=?E(G+KbPeShVmYq3t6w9z$C&F)J@McQf"` + Name string `env:"APP_NAME,default=Goweb"` + Environment Env `env:"APP_ENV,default=local"` + EncryptionKey string `env:"APP_ENCRYPTION_KEY,default=?E(G+KbPeShVmYq3t6w9z$C&F)J@McQf"` + Timeout time.Duration `env:"APP_TIMEOUT,default=20s"` } type CacheConfig struct { @@ -45,7 +46,7 @@ type CacheConfig struct { Password string `env:"CACHE_PASSWORD"` MaxAge struct { StaticFile time.Duration `env:"CACHE_MAX_AGE_STATIC_FILE,default=4380h"` - Page time.Duration `env:"CACHE_STATIC_FILE_MAX_AGE,default=24h"` + Page time.Duration `env:"CACHE_MAX_PAGE_PAGE,default=24h"` } } diff --git a/controllers/controller.go b/controllers/controller.go index a7b1db7..ae330c9 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -2,7 +2,6 @@ package controllers import ( "bytes" - "context" "fmt" "html/template" "net/http" @@ -45,6 +44,8 @@ func NewController(c *container.Container) Controller { } } +// TODO: Audit error handling (ie NewHTTPError) + func (t *Controller) RenderPage(c echo.Context, p Page) error { if p.Name == "" { c.Logger().Error("Page render failed due to missing name") @@ -78,13 +79,12 @@ func (t *Controller) cachePage(c echo.Context, p Page) { p.Cache.MaxAge = t.Container.Config.Cache.MaxAge.Page } - ctx := context.Background() // TODO: make this real key := c.Request().URL.String() opts := &store.Options{ Expiration: p.Cache.MaxAge, Tags: p.Cache.Tags, } - err := t.Container.Cache.Set(ctx, key, "my-value", opts) + err := t.Container.Cache.Set(c.Request().Context(), key, "my-value", opts) if err != nil { c.Logger().Errorf("Failed to cache page: %s", key) c.Logger().Error(err) diff --git a/controllers/page.go b/controllers/page.go index fa716e5..0c0fea5 100644 --- a/controllers/page.go +++ b/controllers/page.go @@ -8,7 +8,7 @@ import ( "goweb/msg" "goweb/pager" - "github.com/labstack/echo/v4/middleware" + echomw "github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4" ) @@ -53,7 +53,7 @@ func NewPage(c echo.Context) Page { p.IsHome = p.Path == "/" - if csrf := c.Get(middleware.DefaultCSRFConfig.ContextKey); csrf != nil { + if csrf := c.Get(echomw.DefaultCSRFConfig.ContextKey); csrf != nil { p.CSRF = csrf.(string) } diff --git a/controllers/router.go b/controllers/router.go index 2f7a2fa..e943f81 100644 --- a/controllers/router.go +++ b/controllers/router.go @@ -28,7 +28,9 @@ func BuildRouter(c *container.Container) { c.Web.Use(echomw.CSRFWithConfig(echomw.CSRFConfig{ TokenLookup: "form:csrf", })) - + c.Web.Use(echomw.TimeoutWithConfig(echomw.TimeoutConfig{ + Timeout: c.Config.App.Timeout, + })) // 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