/
buckets_view.go
42 lines (36 loc) · 953 Bytes
/
buckets_view.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
package s3manager
import (
"fmt"
"html/template"
"io/fs"
"net/http"
"github.com/minio/minio-go/v7"
)
// HandleBucketsView renders all buckets on an HTML page.
func HandleBucketsView(s3 S3, templates fs.FS, allowDelete bool) http.HandlerFunc {
type pageData struct {
Buckets []minio.BucketInfo
AllowDelete bool
}
return func(w http.ResponseWriter, r *http.Request) {
buckets, err := s3.ListBuckets(r.Context())
if err != nil {
handleHTTPError(w, fmt.Errorf("error listing buckets: %w", err))
return
}
data := pageData{
Buckets: buckets,
AllowDelete: allowDelete,
}
t, err := template.ParseFS(templates, "layout.html.tmpl", "buckets.html.tmpl")
if err != nil {
handleHTTPError(w, fmt.Errorf("error parsing template files: %w", err))
return
}
err = t.ExecuteTemplate(w, "layout", data)
if err != nil {
handleHTTPError(w, fmt.Errorf("error executing template: %w", err))
return
}
}
}