-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
58 lines (47 loc) · 1.39 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"embed"
"html/template"
"net/http"
"os"
"github.com/apex/log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
//go:embed templates
var tmpl embed.FS
func (s *server) list() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Info("list")
t, err := template.ParseFS(tmpl, "templates/*.html")
if err != nil {
log.WithError(err).Fatal("Failed to parse templates")
}
// https://aws.github.io/aws-sdk-go-v2/docs/code-examples/dynamodb/scanitems/
records, err := s.client.Scan(context.TODO(), &dynamodb.ScanInput{
TableName: aws.String(os.Getenv("TABLE_NAME")),
})
if err != nil {
log.WithError(err).Fatal("couldn't get records")
}
log.WithField("table", os.Getenv("TABLE_NAME")).Info("got records")
var selection []Record
err = attributevalue.UnmarshalListOfMaps(records.Items, &selection)
if err != nil {
log.WithError(err).Fatal("couldn't parse records")
}
log.WithField("count", len(selection)).Info("parsed records")
w.Header().Set("Content-Type", "text/html")
err = t.ExecuteTemplate(w, "index.html", struct {
Selection []Record
}{
selection,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.WithError(err).Fatal("Failed to execute templates")
}
}
}