s3manager-web/internal/app/s3manager/buckets_view.go

34 lines
810 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"
2018-03-14 21:53:35 +01:00
"path/filepath"
2017-05-25 18:33:44 +02:00
"github.com/pkg/errors"
2017-04-03 14:08:01 +02:00
)
2018-05-31 16:10:41 +02:00
// HandleBucketsView renders all buckets on an HTML page.
func HandleBucketsView(s3 S3, tmplDir string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2018-03-14 21:53:35 +01:00
buckets, err := s3.ListBuckets()
2017-04-03 14:08:01 +02:00
if err != nil {
2018-03-14 21:53:35 +01:00
handleHTTPError(w, errors.Wrap(err, "error listing buckets"))
2017-04-03 14:08:01 +02:00
return
}
2018-03-14 21:53:35 +01:00
l := filepath.Join(tmplDir, "layout.html.tmpl")
p := filepath.Join(tmplDir, "buckets.html.tmpl")
t, err := template.ParseFiles(l, p)
2017-04-03 14:08:01 +02:00
if err != nil {
2018-05-22 22:56:01 +02:00
handleHTTPError(w, errors.Wrap(err, "error parsing template files"))
2017-04-03 14:08:01 +02:00
return
}
err = t.ExecuteTemplate(w, "layout", buckets)
if err != nil {
2018-05-22 22:56:01 +02:00
handleHTTPError(w, errors.Wrap(err, "error executing template"))
2017-04-03 14:08:01 +02:00
return
}
2018-05-31 16:10:41 +02:00
}
2017-04-03 14:08:01 +02:00
}