diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-04 14:50:30 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-04 16:59:10 +0900 |
| commit | 264fa15fb2ba5f0b9636cda44b64deb3c56aa99d (patch) | |
| tree | 61de974c9ed1aa5b94b6d5900fdb8e3a2d4b3efd /backend/admin/renderer.go | |
| parent | 1b794787a5194405cd99d80981008e20aa0e953d (diff) | |
| download | phperkaigi-2025-albatross-264fa15fb2ba5f0b9636cda44b64deb3c56aa99d.tar.gz phperkaigi-2025-albatross-264fa15fb2ba5f0b9636cda44b64deb3c56aa99d.tar.zst phperkaigi-2025-albatross-264fa15fb2ba5f0b9636cda44b64deb3c56aa99d.zip | |
feat(backend): serve /admin/* pages from api-server
Diffstat (limited to 'backend/admin/renderer.go')
| -rw-r--r-- | backend/admin/renderer.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/backend/admin/renderer.go b/backend/admin/renderer.go new file mode 100644 index 0000000..468677f --- /dev/null +++ b/backend/admin/renderer.go @@ -0,0 +1,49 @@ +package admin + +import ( + "embed" + "html/template" + "io" + "net/http" + + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" +) + +var ( + //go:embed templates + templatesFS embed.FS + //go:embed assets + assetsFS embed.FS +) + +type Renderer struct { + templates map[string]*template.Template +} + +func NewRenderer() *Renderer { + return &Renderer{ + templates: make(map[string]*template.Template), + } +} + +func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + tmpl, ok := r.templates[name] + if !ok { + t, err := template.ParseFS(templatesFS, "templates/base.html", "templates/"+name+".html") + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) + } + r.templates[name] = t + tmpl = t + } + return tmpl.ExecuteTemplate(w, name+".html", data) +} + +func newAssetsMiddleware() echo.MiddlewareFunc { + return middleware.StaticWithConfig(middleware.StaticConfig{ + Root: "/assets", + Filesystem: http.FS(assetsFS), + IgnoreBase: true, + }) +} |
