footer: Add a semi-flexible template system
This commit is contained in:
parent
bda763725e
commit
021fc3cc7e
26 changed files with 239 additions and 208 deletions
|
|
@ -3,14 +3,23 @@ package httpx
|
|||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// WriteHTML writes a html response of type T to w.
|
||||
// If an error occured, writes an error response instead.
|
||||
func WriteHTML[T any](result T, err error, template *template.Template, templateName string, w http.ResponseWriter, r *http.Request) {
|
||||
func WriteHTML[T any](result T, err error, template *template.Template, templateName string, w http.ResponseWriter, r *http.Request) (e error) {
|
||||
// log any error that occurs;
|
||||
defer func() {
|
||||
if e != nil {
|
||||
zerolog.Ctx(r.Context()).Err(e).Str("path", r.URL.String()).Msg("error rendering template")
|
||||
}
|
||||
}()
|
||||
|
||||
// intercept any errors
|
||||
if HTMLInterceptor.Intercept(w, r, err) {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// write out the response as html
|
||||
|
|
@ -23,9 +32,9 @@ func WriteHTML[T any](result T, err error, template *template.Template, template
|
|||
|
||||
// and return the template
|
||||
if templateName != "" {
|
||||
template.ExecuteTemplate(minifier, templateName, result)
|
||||
return template.ExecuteTemplate(minifier, templateName, result)
|
||||
} else {
|
||||
template.Execute(minifier, result)
|
||||
return template.Execute(minifier, result)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
//go:build nominify
|
||||
|
||||
package httpx
|
||||
|
||||
import (
|
||||
|
|
|
|||
27
pkg/httpx/html_minify_off.go
Normal file
27
pkg/httpx/html_minify_off.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
//go:build !nominify
|
||||
|
||||
package httpx
|
||||
|
||||
import "io"
|
||||
|
||||
// MinifyHTMLWriter wraps the given io.Writer to minify the given html instead.
|
||||
// The writer must be closed explicitly.
|
||||
//
|
||||
// Specific environments may chose to disable http minification, in which case MinifyHTMLWriter becomes the identity function.
|
||||
func MinifyHTMLWriter(dest io.Writer) io.WriteCloser {
|
||||
return noop{Writer: dest}
|
||||
}
|
||||
|
||||
type noop struct {
|
||||
io.Writer
|
||||
}
|
||||
|
||||
func (noop) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MinifyHTML minifies the html source.
|
||||
// If an error occurs, returns the unmodified source instead.
|
||||
func MinifyHTML(source []byte) []byte {
|
||||
return source
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue