aboutsummaryrefslogtreecommitdiff
path: root/echo/echo_default.go
diff options
context:
space:
mode:
Diffstat (limited to 'echo/echo_default.go')
-rw-r--r--echo/echo_default.go37
1 files changed, 28 insertions, 9 deletions
diff --git a/echo/echo_default.go b/echo/echo_default.go
index ab163ff..895ee6e 100644
--- a/echo/echo_default.go
+++ b/echo/echo_default.go
@@ -5,7 +5,9 @@ import (
5 "crypto/tls" 5 "crypto/tls"
6 "fmt" 6 "fmt"
7 "html/template" 7 "html/template"
8 "io/fs"
8 "net/http" 9 "net/http"
10 "path"
9 "sync" 11 "sync"
10 12
11 glmw "code.crute.us/mcrute/golib/echo/middleware" 13 glmw "code.crute.us/mcrute/golib/echo/middleware"
@@ -20,10 +22,10 @@ type EchoConfig struct {
20 Debug bool 22 Debug bool
21 BindAddress string 23 BindAddress string
22 BindTLSAddress string 24 BindTLSAddress string
23 TLSCert string 25 TLSCacheDir string
24 TLSKey string
25 TrustedProxyIPRanges []string 26 TrustedProxyIPRanges []string
26 TemplatePath *string 27 EmbeddedTemplates fs.FS
28 DiskTemplates fs.FS
27 TemplateGlob *string 29 TemplateGlob *string
28 TemplateFunctions template.FuncMap 30 TemplateFunctions template.FuncMap
29 CombinedHostLogFile string 31 CombinedHostLogFile string
@@ -34,6 +36,7 @@ type EchoConfig struct {
34type EchoWrapper struct { 36type EchoWrapper struct {
35 *echo.Echo 37 *echo.Echo
36 tlsServer http.Server 38 tlsServer http.Server
39 templateFS fs.FS
37 bindAddress string 40 bindAddress string
38 ocspErrors chan gltls.OcspError 41 ocspErrors chan gltls.OcspError
39 ocspManager *gltls.OcspManager 42 ocspManager *gltls.OcspManager
@@ -51,7 +54,7 @@ func (w *EchoWrapper) Init() error {
51} 54}
52 55
53func (w *EchoWrapper) CachedStaticRoute(prefix, path string) { 56func (w *EchoWrapper) CachedStaticRoute(prefix, path string) {
54 w.Group(prefix, glmw.CacheOneMonthMiddleware).Static("/", path) 57 StaticFS(w.GET, w.templateFS, prefix, path, glmw.CacheOneMonthMiddleware)
55} 58}
56 59
57func (w *EchoWrapper) OcspErrors() chan gltls.OcspError { 60func (w *EchoWrapper) OcspErrors() chan gltls.OcspError {
@@ -103,6 +106,10 @@ func (w *EchoWrapper) ServeTLS(ctx context.Context, wg *sync.WaitGroup) error {
103 ) 106 )
104} 107}
105 108
109func (w *EchoWrapper) GetTemplateFS() fs.FS {
110 return w.templateFS
111}
112
106// NewDefaultEchoWithConfig builds a wrapper around an Echo instance and 113// NewDefaultEchoWithConfig builds a wrapper around an Echo instance and
107// configures it in the default way that it should probably be configured in 114// configures it in the default way that it should probably be configured in
108// all cases. The struct returned from this function can be treated like a 115// all cases. The struct returned from this function can be treated like a
@@ -120,11 +127,22 @@ func NewDefaultEchoWithConfig(c EchoConfig) (*EchoWrapper, error) {
120 return nil, fmt.Errorf("Error building XFF IP extractor: %w", err) 127 return nil, fmt.Errorf("Error building XFF IP extractor: %w", err)
121 } 128 }
122 129
130 // Use templates from disk in debug mode and the embedded ones that are
131 // built-in to the binary for prod mode.
132 var templates fs.FS
133 if c.DiskTemplates != nil && c.Debug { // Debug Mode
134 templates = c.DiskTemplates
135 } else if c.EmbeddedTemplates != nil && !c.Debug { // Prod Mode
136 templates = c.EmbeddedTemplates
137 } else {
138 return nil, fmt.Errorf("No templates available for use")
139 }
140
123 // Only install template handlers if the path and glob are set 141 // Only install template handlers if the path and glob are set
124 if c.TemplatePath != nil && c.TemplateGlob != nil { 142 if templates != nil && c.TemplateGlob != nil {
125 e.HTTPErrorHandler = ErrorHandler(*c.TemplatePath, c.TemplateFunctions) 143 e.HTTPErrorHandler = ErrorHandler(templates, c.TemplateFunctions)
126 144
127 tr, err := NewTemplateRenderer(*c.TemplatePath, *c.TemplateGlob, c.TemplateFunctions) 145 tr, err := NewTemplateRenderer(templates, *c.TemplateGlob, c.TemplateFunctions)
128 if err != nil { 146 if err != nil {
129 return nil, fmt.Errorf("Error loading template renderer: %w", err) 147 return nil, fmt.Errorf("Error loading template renderer: %w", err)
130 } 148 }
@@ -146,8 +164,8 @@ func NewDefaultEchoWithConfig(c EchoConfig) (*EchoWrapper, error) {
146 164
147 cmek := make(chan gltls.OcspError) 165 cmek := make(chan gltls.OcspError)
148 cm := &gltls.OcspManager{ 166 cm := &gltls.OcspManager{
149 CertPath: c.TLSCert, 167 CertPath: path.Join(c.TLSCacheDir, "cert.pem"),
150 KeyPath: c.TLSKey, 168 KeyPath: path.Join(c.TLSCacheDir, "key.pem"),
151 Errors: cmek, 169 Errors: cmek,
152 } 170 }
153 171
@@ -181,5 +199,6 @@ func NewDefaultEchoWithConfig(c EchoConfig) (*EchoWrapper, error) {
181 bindAddress: c.BindAddress, 199 bindAddress: c.BindAddress,
182 ocspErrors: cmek, 200 ocspErrors: cmek,
183 ocspManager: cm, 201 ocspManager: cm,
202 templateFS: templates,
184 }, nil 203 }, nil
185} 204}