From 7b7bdcf57a5fa1e02041e3ef563c55f31d908f67 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 17 May 2022 14:39:42 -0600 Subject: Initial implementation of admin assets page --- srv/src/api/api.go | 2 ++ srv/src/api/render.go | 34 ++++++++++++++++++++++++++++++++++ srv/src/api/tpl/admin/assets.html | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 srv/src/api/tpl/admin/assets.html (limited to 'srv/src/api') diff --git a/srv/src/api/api.go b/srv/src/api/api.go index 0184ef1..8d54d46 100644 --- a/srv/src/api/api.go +++ b/srv/src/api/api.go @@ -200,6 +200,8 @@ func (a *api) handler() http.Handler { mux.Handle("/v2/assets/", a.servePostAssetHandler()) + mux.Handle("/v2/admin/assets.html", a.renderAdminAssets()) + var globalHandler http.Handler = mux globalHandler = setLoggerMiddleware(a.params.Logger, globalHandler) diff --git a/srv/src/api/render.go b/srv/src/api/render.go index 0f45211..8fc2cb6 100644 --- a/srv/src/api/render.go +++ b/srv/src/api/render.go @@ -39,6 +39,10 @@ func (a *api) mustParseTpl(name string) *template.Template { tpl := template.New("").Funcs(template.FuncMap{ "BlogURL": blogURL, + "AssetURL": func(path string) string { + path = filepath.Join("assets", path) + return blogURL(path) + }, }) tpl = template.Must(tpl.Parse(mustRead(name))) @@ -192,3 +196,33 @@ func (a *api) renderDumbHandler(tplName string) http.Handler { } }) } + +func (a *api) renderAdminAssets() http.Handler { + + tpl := a.mustParseTpl("admin/assets.html") + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + ids, err := a.params.PostAssetStore.List() + + if err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("getting list of asset ids: %w", err), + ) + return + } + + tplData := struct { + IDs []string + }{ + IDs: ids, + } + + if err := tpl.Execute(rw, tplData); err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("rendering: %w", err), + ) + return + } + }) +} diff --git a/srv/src/api/tpl/admin/assets.html b/srv/src/api/tpl/admin/assets.html new file mode 100644 index 0000000..d871a3e --- /dev/null +++ b/srv/src/api/tpl/admin/assets.html @@ -0,0 +1,18 @@ +{{ define "body" }} + + + + {{ range .IDs }} + + + + + {{ end }} + +
{{ . }} + Delete (TODO) +
+ +{{ end }} + +{{ template "base.html" . }} -- cgit v1.2.3