Initial implementation of admin assets page

This commit is contained in:
Brian Picciano 2022-05-17 14:39:42 -06:00
parent 9a67ef9211
commit 7b7bdcf57a
5 changed files with 86 additions and 60 deletions

View File

@ -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)

View File

@ -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
}
})
}

View File

@ -0,0 +1,18 @@
{{ define "body" }}
<table>
{{ range .IDs }}
<tr>
<td><a href="{{ AssetURL . }}" target="_blank">{{ . }}</a></td>
<td>
Delete (TODO)
</td>
</tr>
{{ end }}
</table>
{{ end }}
{{ template "base.html" . }}

View File

@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"sync"
)
var (
@ -28,6 +27,9 @@ type AssetStore interface {
// Delete's the body stored for the id, if any.
Delete(id string) error
// List returns all ids which are currently stored.
List() ([]string, error)
}
type assetStore struct {
@ -86,68 +88,27 @@ func (s *assetStore) Delete(id string) error {
return err
}
////////////////////////////////////////////////////////////////////////////////
func (s *assetStore) List() ([]string, error) {
type cachedAssetStore struct {
inner AssetStore
m sync.Map
}
// NewCachedAssetStore wraps an AssetStore in an in-memory cache.
func NewCachedAssetStore(assetStore AssetStore) AssetStore {
return &cachedAssetStore{
inner: assetStore,
}
}
func (s *cachedAssetStore) Set(id string, from io.Reader) error {
buf := new(bytes.Buffer)
from = io.TeeReader(from, buf)
if err := s.inner.Set(id, from); err != nil {
return err
}
s.m.Store(id, buf.Bytes())
return nil
}
func (s *cachedAssetStore) Get(id string, into io.Writer) error {
if bodyI, ok := s.m.Load(id); ok {
if err, ok := bodyI.(error); ok {
return err
}
if _, err := io.Copy(into, bytes.NewReader(bodyI.([]byte))); err != nil {
return fmt.Errorf("writing body to io.Writer: %w", err)
}
return nil
}
buf := new(bytes.Buffer)
into = io.MultiWriter(into, buf)
if err := s.inner.Get(id, into); errors.Is(err, ErrAssetNotFound) {
s.m.Store(id, err)
return err
} else if err != nil {
return err
}
s.m.Store(id, buf.Bytes())
return nil
}
func (s *cachedAssetStore) Delete(id string) error {
if err := s.inner.Delete(id); err != nil {
return err
}
s.m.Delete(id)
return nil
rows, err := s.db.Query(`SELECT id FROM assets ORDER BY id ASC`)
if err != nil {
return nil, fmt.Errorf("querying: %w", err)
}
defer rows.Close()
var ids []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return nil, fmt.Errorf("scanning row: %w", err)
}
ids = append(ids, id)
}
return ids, nil
}

View File

@ -65,16 +65,27 @@ func TestAssetStore(t *testing.T) {
assert.NoError(t, h.store.Delete("bar"))
h.assertNotFound(t, "foo")
h.assertNotFound(t, "bar")
// test list
ids, err := h.store.List()
assert.NoError(t, err)
assert.Empty(t, ids)
err = h.store.Set("foo", bytes.NewBufferString("FOOFOO"))
assert.NoError(t, err)
err = h.store.Set("foo", bytes.NewBufferString("FOOFOO"))
assert.NoError(t, err)
err = h.store.Set("bar", bytes.NewBufferString("FOOFOO"))
assert.NoError(t, err)
ids, err = h.store.List()
assert.NoError(t, err)
assert.Equal(t, []string{"bar", "foo"}, ids)
}
t.Run("sql", func(t *testing.T) {
h := newAssetTestHarness(t)
testAssetStore(t, h)
})
t.Run("mem", func(t *testing.T) {
h := newAssetTestHarness(t)
h.store = NewCachedAssetStore(h.store)
testAssetStore(t, h)
})
}