diff --git a/ui/amcontext.go b/ui/amcontext.go index 5aa3320..fd13693 100644 --- a/ui/amcontext.go +++ b/ui/amcontext.go @@ -18,8 +18,10 @@ import ( type AmContext interface { RC() int + OutputType() string Render(string) error SubRender(string) ([]byte, error) + SetOutputType(string) SetRC(int) URLPath() string VarMap() jet.VarMap @@ -29,12 +31,17 @@ type amContext struct { echoContext echo.Context httprc int rendervars jet.VarMap + outputType string } func (c *amContext) RC() int { return c.httprc } +func (c *amContext) OutputType() string { + return c.outputType +} + func (c *amContext) Render(name string) error { return c.echoContext.Render(c.httprc, name, c) } @@ -49,6 +56,10 @@ func (c *amContext) SubRender(name string) ([]byte, error) { return buf.Bytes(), err } +func (c *amContext) SetOutputType(typ string) { + c.outputType = typ +} + func (c *amContext) SetRC(rc int) { c.httprc = rc } @@ -66,6 +77,7 @@ func NewAmContext(ctxt echo.Context) AmContext { echoContext: ctxt, httprc: http.StatusOK, rendervars: make(jet.VarMap), + outputType: "", } ctxt.Set("amsterdam_context", &rc) return &rc diff --git a/ui/images.go b/ui/images.go new file mode 100644 index 0000000..a8a3863 --- /dev/null +++ b/ui/images.go @@ -0,0 +1,36 @@ +/* + * Amsterdam Web Communities System + * Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved + * + * This Source Code Form is subject to the terms of the Mozilla Public + * 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 + +import ( + "embed" + "fmt" + "mime" + "net/http" + "strings" +) + +//go:embed static_images/* +var static_images embed.FS + +func AmServeImage(ctxt AmContext) (string, any, error) { + components := strings.SplitAfter(ctxt.URLPath(), "/") + var err error = nil + if len(components) == 2 && components[0] == "builtin" { + var b []byte + b, err = static_images.ReadFile(fmt.Sprintf("static_images/%s", components[1])) + if err == nil { + mtype := mime.TypeByExtension(components[1][strings.LastIndex(components[1], "."):]) + ctxt.SetOutputType(mtype) + return "bytes", b, nil + } + } + ctxt.SetRC(http.StatusNotFound) + return "string", fmt.Sprintf("File not found: %s", ctxt.URLPath()), err +} diff --git a/ui/render_wrap.go b/ui/render_wrap.go index 38e44c3..730dbd4 100644 --- a/ui/render_wrap.go +++ b/ui/render_wrap.go @@ -20,6 +20,8 @@ func AmWrap(myfunc func(AmContext) (string, any, error)) echo.HandlerFunc { what, rc, err := myfunc(amctxt) if err == nil { switch what { + case "bytes": + err = ctxt.Blob(amctxt.RC(), amctxt.OutputType(), rc.([]byte)) case "string": err = ctxt.String(amctxt.RC(), fmt.Sprintf("%v", rc)) case "template": diff --git a/ui/static_images/powered-by-amsterdam.png b/ui/static_images/powered-by-amsterdam.png new file mode 100644 index 0000000..47cef09 Binary files /dev/null and b/ui/static_images/powered-by-amsterdam.png differ