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
+8 -1
View File
@@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
// Package config contains support for Amsterdam site-wide configuration data.
package config package config
import ( import (
@@ -14,6 +16,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
// AmConfig holds the configuration of the application as read from YAML.
type AmConfig struct { type AmConfig struct {
Site struct { Site struct {
Title string `yaml:"title"` Title string `yaml:"title"`
@@ -23,10 +26,14 @@ type AmConfig struct {
//go:embed default.yaml //go:embed default.yaml
var defaultConfigData []byte var defaultConfigData []byte
// GlobalConfig holds the global configuration.
var GlobalConfig AmConfig var GlobalConfig AmConfig
// init prepares the default configuration for the application.
func init() { func init() {
var defaultConfig AmConfig var defaultConfig AmConfig
yaml.Unmarshal(defaultConfigData, &defaultConfig) if err := yaml.Unmarshal(defaultConfigData, &defaultConfig); err != nil {
panic(err) // can't happen
}
GlobalConfig = defaultConfig GlobalConfig = defaultConfig
} }
+17 -2
View File
@@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
// Package main contains the high-level Amsterdam logic.
package main package main
import ( import (
@@ -17,6 +19,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// init sets up the initial configuration for Logrus logging.
func init() { func init() {
log.SetFormatter(&log.TextFormatter{ log.SetFormatter(&log.TextFormatter{
FullTimestamp: true, FullTimestamp: true,
@@ -25,6 +28,12 @@ func init() {
log.SetLevel(log.InfoLevel) log.SetLevel(log.InfoLevel)
} }
/* toglog converts a Logrus logging level to a glog one.
* Parameters:
* l - The Logrus log level to be converted.
* Returns:
* The equivalent glog log level.
*/
func toglog(l log.Level) glog.Lvl { func toglog(l log.Level) glog.Lvl {
switch l { switch l {
case log.DebugLevel: case log.DebugLevel:
@@ -40,6 +49,12 @@ func toglog(l log.Level) glog.Lvl {
} }
} }
/* fromglog converts a glog logging level to a Logrus one.
* Parameters:
* l - The glog log level to be converted.
* Returns:
* The equivalent Logrus log level.
*/
func fromglog(l glog.Lvl) log.Level { func fromglog(l glog.Lvl) log.Level {
switch l { switch l {
case glog.DEBUG: case glog.DEBUG:
@@ -55,7 +70,7 @@ func fromglog(l glog.Lvl) log.Level {
} }
} }
// EchoLogrusAdapter implements echo.Logger using logrus // EchoLogrusAdapter implements echo.Logger using logrus.
type EchoLogrusAdapter struct{} type EchoLogrusAdapter struct{}
func (l *EchoLogrusAdapter) Output() io.Writer { return log.StandardLogger().Out } func (l *EchoLogrusAdapter) Output() io.Writer { return log.StandardLogger().Out }
@@ -87,7 +102,7 @@ func (l *EchoLogrusAdapter) Panicf(format string, args ...interface{}) { log.Pan
func (l *EchoLogrusAdapter) Panicj(j glog.JSON) { log.WithFields(log.Fields(j)).Panic() } func (l *EchoLogrusAdapter) Panicj(j glog.JSON) { log.WithFields(log.Fields(j)).Panic() }
func (l *EchoLogrusAdapter) SetHeader(h string) {} func (l *EchoLogrusAdapter) SetHeader(h string) {}
// Custom Logrus middleware // LogrusMiddleware installs Logrus logging into the Echo middleware chain.
func LogrusMiddleware(next echo.HandlerFunc) echo.HandlerFunc { func LogrusMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
start := time.Now() start := time.Now()
+4
View File
@@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
// Package main contains the high-level Amsterdam logic.
package main package main
import ( import (
@@ -14,6 +16,7 @@ import (
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
) )
// setupEcho creates, configures, and returns a new Echo instance.
func setupEcho() *echo.Echo { func setupEcho() *echo.Echo {
e := echo.New() e := echo.New()
e.Logger = &EchoLogrusAdapter{} e.Logger = &EchoLogrusAdapter{}
@@ -30,6 +33,7 @@ func setupEcho() *echo.Echo {
return e return e
} }
// main is Ye Olde Main Function.
func main() { func main() {
e := setupEcho() e := setupEcho()
+35
View File
@@ -6,6 +6,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. * 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 package ui
import ( import (
@@ -16,6 +18,7 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
// AmContext is the interface for Amsterdam's wapper context that exposes the required functionality.
type AmContext interface { type AmContext interface {
RC() int RC() int
OutputType() string OutputType() string
@@ -27,6 +30,7 @@ type AmContext interface {
VarMap() jet.VarMap VarMap() jet.VarMap
} }
// amContext is the internal structure that implements AmContext.
type amContext struct { type amContext struct {
echoContext echo.Context echoContext echo.Context
httprc int httprc int
@@ -34,18 +38,33 @@ type amContext struct {
outputType string outputType string
} }
// RC returns the HTTP result code for the current operation.
func (c *amContext) RC() int { func (c *amContext) RC() int {
return c.httprc return c.httprc
} }
// OutputType returns the MIME output type set for the current operation.
func (c *amContext) OutputType() string { func (c *amContext) OutputType() string {
return c.outputType 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 { func (c *amContext) Render(name string) error {
return c.echoContext.Render(c.httprc, name, c) 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) { func (c *amContext) SubRender(name string) ([]byte, error) {
view, err := views.GetTemplate(name) view, err := views.GetTemplate(name)
if err != nil { if err != nil {
@@ -56,22 +75,32 @@ func (c *amContext) SubRender(name string) ([]byte, error) {
return buf.Bytes(), err return buf.Bytes(), err
} }
// SetOutputType sets the MIME output type for the current operation.
func (c *amContext) SetOutputType(typ string) { func (c *amContext) SetOutputType(typ string) {
c.outputType = typ c.outputType = typ
} }
// SetRC sets the HTTP result code for the current operation.
func (c *amContext) SetRC(rc int) { func (c *amContext) SetRC(rc int) {
c.httprc = rc c.httprc = rc
} }
// URLPath returns the path component of the request URL.
func (c *amContext) URLPath() string { func (c *amContext) URLPath() string {
return c.echoContext.Request().URL.Path return c.echoContext.Request().URL.Path
} }
// VarMap provides access to the Jet variable map for setting variable data.
func (c *amContext) VarMap() jet.VarMap { func (c *amContext) VarMap() jet.VarMap {
return c.rendervars 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 { func NewAmContext(ctxt echo.Context) AmContext {
rc := amContext{ rc := amContext{
echoContext: ctxt, echoContext: ctxt,
@@ -83,6 +112,12 @@ func NewAmContext(ctxt echo.Context) AmContext {
return &rc 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 { func AmContextFromEchoContext(ctxt echo.Context) AmContext {
myctxt := ctxt.Get("amsterdam_context") myctxt := ctxt.Get("amsterdam_context")
if myctxt != nil { 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 * 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/. * 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 package ui
import ( import (
@@ -19,10 +21,24 @@ import (
//go:embed static_images/* //go:embed static_images/*
var static_images embed.FS 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 { func mimeTypeFromFilename(filename string) string {
return mime.TypeByExtension(filename[strings.LastIndex(filename, "."):]) 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) { func AmServeImage(ctxt AmContext) (string, any, error) {
components := strings.SplitAfter(ctxt.URLPath(), "/") components := strings.SplitAfter(ctxt.URLPath(), "/")
var err error = nil var err error = nil
@@ -35,5 +51,6 @@ func AmServeImage(ctxt AmContext) (string, any, error) {
} }
} }
ctxt.SetRC(http.StatusNotFound) ctxt.SetRC(http.StatusNotFound)
// TODO: improve this error reporting
return "string", fmt.Sprintf("File not found: %s", ctxt.URLPath()), err 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 * 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/. * 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 package ui
import ( import (
@@ -14,6 +16,13 @@ import (
"github.com/labstack/echo/v4" "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 { func AmWrap(myfunc func(AmContext) (string, any, error)) echo.HandlerFunc {
return func(ctxt echo.Context) error { return func(ctxt echo.Context) error {
amctxt := NewAmContext(ctxt) 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) err = fmt.Errorf("unknown rendering type: %s", what)
} }
if err != nil { if err != nil {
ctxt.Logger().Error("Rendering error: %v", err) ctxt.Logger().Errorf("Rendering error: %v", err)
} }
} else { } else {
ctxt.Logger().Error("Page function error: %v", err) ctxt.Logger().Errorf("Page function error: %v", err)
} }
return 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 * 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/. * 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 package ui
import ( import (
@@ -16,18 +18,29 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
// views is the main Jet template repository.
var views = jet.NewSet( var views = jet.NewSet(
jet.NewOSFileSystemLoader("./views"), jet.NewOSFileSystemLoader("./views"),
jet.DevelopmentMode(true), jet.DevelopmentMode(true),
) )
// init adds additional configuration for the views object.
func init() { func init() {
views.AddGlobal("GlobalConfig", config.GlobalConfig) 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 { func (r *TemplateRenderer) Render(w io.Writer, name string, data any, c echo.Context) error {
view, err := views.GetTemplate(name) view, err := views.GetTemplate(name)
if err != nil { if err != nil {