godoc'd the source

This commit is contained in:
2025-09-14 22:05:17 -06:00
parent c6391a456f
commit b10b4e93f2
7 changed files with 107 additions and 7 deletions
+35
View File
@@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package ui holds the support for the Amsterdam user interface, wrapping Echo and Jet templates.
package ui
import (
@@ -16,6 +18,7 @@ import (
"github.com/labstack/echo/v4"
)
// AmContext is the interface for Amsterdam's wapper context that exposes the required functionality.
type AmContext interface {
RC() int
OutputType() string
@@ -27,6 +30,7 @@ type AmContext interface {
VarMap() jet.VarMap
}
// amContext is the internal structure that implements AmContext.
type amContext struct {
echoContext echo.Context
httprc int
@@ -34,18 +38,33 @@ type amContext struct {
outputType string
}
// RC returns the HTTP result code for the current operation.
func (c *amContext) RC() int {
return c.httprc
}
// OutputType returns the MIME output type set for the current operation.
func (c *amContext) OutputType() string {
return c.outputType
}
/* Render renders a template to the output. Called at the top level only.
* Parameters:
* name = The name of the tempate to be rendered.
* Returns:
* Standard Go error status.
*/
func (c *amContext) Render(name string) error {
return c.echoContext.Render(c.httprc, name, c)
}
/* SubRender renders a subtemplate to the output.
* Parameters:
* name = The name of the template to be rendered.
* Returns:
* Byte array with the rendered data to be output
* Standard Go error status
*/
func (c *amContext) SubRender(name string) ([]byte, error) {
view, err := views.GetTemplate(name)
if err != nil {
@@ -56,22 +75,32 @@ func (c *amContext) SubRender(name string) ([]byte, error) {
return buf.Bytes(), err
}
// SetOutputType sets the MIME output type for the current operation.
func (c *amContext) SetOutputType(typ string) {
c.outputType = typ
}
// SetRC sets the HTTP result code for the current operation.
func (c *amContext) SetRC(rc int) {
c.httprc = rc
}
// URLPath returns the path component of the request URL.
func (c *amContext) URLPath() string {
return c.echoContext.Request().URL.Path
}
// VarMap provides access to the Jet variable map for setting variable data.
func (c *amContext) VarMap() jet.VarMap {
return c.rendervars
}
/* NewAmContext creates a new AmContext wrapping the Echo context.
* Parameters:
* ctxt - The Echo context to be wrapped.
* Returns:
* A new Amsterdam context wrapping that context.
*/
func NewAmContext(ctxt echo.Context) AmContext {
rc := amContext{
echoContext: ctxt,
@@ -83,6 +112,12 @@ func NewAmContext(ctxt echo.Context) AmContext {
return &rc
}
/* AmContextFromEchoContext returns the AmContext associated with an Echo context.
* Parameters:
* ctxt - The Echo context to have the AmContext extracted.
* Returns:
* The associated AmContext, or nil if there is none.
*/
func AmContextFromEchoContext(ctxt echo.Context) AmContext {
myctxt := ctxt.Get("amsterdam_context")
if myctxt != nil {
+17
View File
@@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package ui holds the support for the Amsterdam user interface, wrapping Echo and Jet templates.
package ui
import (
@@ -19,10 +21,24 @@ import (
//go:embed static_images/*
var static_images embed.FS
/* mimeTypeFromFilenane returns the MIME type of a file, given its filename.
* Parameters:
* filaname - The name of the file to be tested.
* Returns:
* The file's inferred MIME type.
*/
func mimeTypeFromFilename(filename string) string {
return mime.TypeByExtension(filename[strings.LastIndex(filename, "."):])
}
/* AmServeImage serves an image from internal storage.
* Parameters:
* ctxt - The AmContext for the request.
* Returns:
* Type of content to be rendered
* Content to be rendered
* Standard Go error return
*/
func AmServeImage(ctxt AmContext) (string, any, error) {
components := strings.SplitAfter(ctxt.URLPath(), "/")
var err error = nil
@@ -35,5 +51,6 @@ func AmServeImage(ctxt AmContext) (string, any, error) {
}
}
ctxt.SetRC(http.StatusNotFound)
// TODO: improve this error reporting
return "string", fmt.Sprintf("File not found: %s", ctxt.URLPath()), err
}
+11 -2
View File
@@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package ui holds the support for the Amsterdam user interface, wrapping Echo and Jet templates.
package ui
import (
@@ -14,6 +16,13 @@ import (
"github.com/labstack/echo/v4"
)
/* AmWrap wraps the Amsterdam handler function in a wrapper that implements the spec for
* Echo handler functions.
* Parameters:
* myfunc - The Amsterdam handler to be wrapped.
* Returns:
* The wrapped function.
*/
func AmWrap(myfunc func(AmContext) (string, any, error)) echo.HandlerFunc {
return func(ctxt echo.Context) error {
amctxt := NewAmContext(ctxt)
@@ -33,10 +42,10 @@ func AmWrap(myfunc func(AmContext) (string, any, error)) echo.HandlerFunc {
err = fmt.Errorf("unknown rendering type: %s", what)
}
if err != nil {
ctxt.Logger().Error("Rendering error: %v", err)
ctxt.Logger().Errorf("Rendering error: %v", err)
}
} else {
ctxt.Logger().Error("Page function error: %v", err)
ctxt.Logger().Errorf("Page function error: %v", err)
}
return err
}
+15 -2
View File
@@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package ui holds the support for the Amsterdam user interface, wrapping Echo and Jet templates.
package ui
import (
@@ -16,18 +18,29 @@ import (
"github.com/labstack/echo/v4"
)
// views is the main Jet template repository.
var views = jet.NewSet(
jet.NewOSFileSystemLoader("./views"),
jet.DevelopmentMode(true),
)
// init adds additional configuration for the views object.
func init() {
views.AddGlobal("GlobalConfig", config.GlobalConfig)
}
type TemplateRenderer struct {
}
// TemplateRenderer is the Renderer instance set into the Echo context at creation time, to render Jet templates.
type TemplateRenderer struct{}
/* Render renders a Jet template to the Echo output stream.
* Parameters:
* w - Echo's output stream writer.
* name - Name of the template to be rendered.
* data - Context data to pass to the template.
* c - The Echo context for the request being processed.
* Returns:
* Standard Go error status.
*/
func (r *TemplateRenderer) Render(w io.Writer, name string, data any, c echo.Context) error {
view, err := views.GetTemplate(name)
if err != nil {