s3manager-web/buckets_view.go

36 lines
790 B
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager
2017-04-03 14:08:01 +02:00
import (
"html/template"
"net/http"
"path"
2017-05-25 18:33:44 +02:00
"github.com/pkg/errors"
2017-04-03 14:08:01 +02:00
)
2017-05-08 23:07:07 +02:00
// BucketsViewHandler renders all buckets on an HTML page.
func BucketsViewHandler(s3 S3) http.Handler {
2017-04-03 14:08:01 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2017-04-07 10:07:23 +02:00
l := path.Join(tmplDirectory, "layout.html.tmpl")
p := path.Join(tmplDirectory, "buckets.html.tmpl")
2017-04-03 14:08:01 +02:00
t, err := template.ParseFiles(l, p)
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, errParsingTemplates))
2017-04-03 14:08:01 +02:00
return
}
buckets, err := s3.ListBuckets()
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, "error listing buckets"))
2017-04-03 14:08:01 +02:00
return
}
err = t.ExecuteTemplate(w, "layout", buckets)
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, errExecutingTemplate))
2017-04-03 14:08:01 +02:00
return
}
})
}