From 72bf9d0a62c8459ce7c832298c723656e7e6d384 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Wed, 18 Oct 2023 12:51:44 -0700 Subject: echo: support more correct disk templates --- echo/echo_default.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/echo/echo_default.go b/echo/echo_default.go index 8440fe2..aa9e32a 100644 --- a/echo/echo_default.go +++ b/echo/echo_default.go @@ -7,6 +7,7 @@ import ( "html/template" "io/fs" "net/http" + "os" "sync" "code.crute.us/mcrute/golib/clients/netbox" @@ -61,6 +62,7 @@ type EchoConfig struct { TrustedProxyIPRanges []string EmbeddedTemplates fs.FS DiskTemplates fs.FS + DiskTemplatesPath string ProvideTemplateStore []WantsTemplateStore TemplateFunctions template.FuncMap CombinedHostLogFile string @@ -218,12 +220,25 @@ func (w *EchoWrapper) configureIpExtractor(c *EchoConfig) error { } func (w *EchoWrapper) configureTemplates(c *EchoConfig) error { - // Use templates from disk in debug mode and the embedded ones that are - // built-in to the binary for prod mode. + // TODO: v1, deprecate DiskTemplates var templates fs.FS - - if c.DiskTemplates != nil && w.Debug { // Debug Mode - templates = c.DiskTemplates + if (c.DiskTemplatesPath != "" || c.DiskTemplates != nil) && w.Debug { // Debug Mode + // If the fs.FS for DiskTemplates is provided use that + if c.DiskTemplates != nil { + w.Logger.Debug("Debug mode, using disk templates from passed fs.FS") + w.Logger.Warn("Using disk templates from an fs.FS is deprecated behavior") + templates = c.DiskTemplates + } else { + // Use templates from disk in debug mode and the embedded ones that are + // built-in to the binary for prod mode. + if _, err := os.Stat(c.DiskTemplatesPath); err == nil { + w.Logger.Debug("Debug mode, using disk templates from filesystem") + templates = os.DirFS(c.DiskTemplatesPath) + } else { + w.Logger.Warnf("Disk templates not found, using embedded templates only") + templates = c.EmbeddedTemplates + } + } } else if c.EmbeddedTemplates != nil { // Prod Mode templates = c.EmbeddedTemplates } -- cgit v1.2.3